0

So I have a very normalized model, and I'm trying to create a single page application in MVC4 which wants to use an entity framework object. My problem is I can't manage to create an entity in EF with the kind of complex mapping that I need (I have checked multiple guides, but I can't seem to make one entity from multiple tables that contain different primary keys... I found a solution using updateable views, but that's really just pushing the abstraction down to the db layer).

So I thought I could create a POCO object using an EF query to create the object, then on insert/update/delete I could just take the POCO data and update the underlying 3 tables.

Well I hit a roadblock just trying to tweak an existing working controller to try and learn what's going on.

Let's imagine I have a working SPA controller that looks like this:

public partial class FooController : DbDataController<aspnetEntities> 
{    
    public IQueryable<Foos> GetFoos() { ... }    
}

I just change it a bit to return my new POCO data object Bar, which let's imagine has the exact same fields as Foo for the moment:

public partial class FooController : DbDataController<aspnetEntities>
{
    public IQueryable<Bars> GetBars() { ... }
}

Over in FooViewModel.js I update the operation name to GetBars, and the type from

var entityType = "Foo:#Models";

to

var entityType = "Bar:#Models";

I hit my operation directly and I get:

OLD <ArrayOfFoo><Foo><Property>true</Property></Foo></ArrayOfFoo>

NEW <ArrayOfBar><Bar><Property>true</Property></Bar></ArrayOfBar>

So the controller looks like it's giving me what I expect, but when I try to put the whole thing together the SPA flashes up:

You must write an attribute 'type'='object' after writing the attribute with local name '__type'.

I'm guessing somehow I need to get type data into KO? I'm not sure where that might be however, I've been crawling through the JS for hours, but I'm not even clear on where it's failing. Any input would be greatly appreciated.

nemesv
  • 138,284
  • 16
  • 416
  • 359
Dio
  • 660
  • 1
  • 9
  • 19
  • are you sure there is no track of the old entity in the various methods to edit, display etc. the entity? __type is an attribute added to the entities that store their type, so ...maybe a kind of type mismatch is found. – Francesco Abbruzzese Apr 15 '12 at 10:09
  • Francesco, there might be, but not on the GetBars() method, but I'm not clear if maybe the JS framework is getting meta data from the service? If so, I'm not actually sure where it's doing that either. I think in truth I don't know enough about SPAs, but this is the technology I'm stuck with so I'm trying to work my way though it. Thanks for the info. – Dio Apr 15 '12 at 17:40

1 Answers1

1

The problem you are experiencing is connected to the fact you are using POCO instead of the standard EF. It should be related to the webapi serializer that somehow doesn't recognize the class as serializable. Anyway it is a bug that will be removed in the RC. Give a look to this thread for workarounds: http://forums.asp.net/t/1773173.aspx/1?You+must+write+an+attribute+type+object+after+writing+the+attribute+with+local+name+__type+

Francesco Abbruzzese
  • 4,139
  • 1
  • 17
  • 18
  • Thanks, it's frustrating but at least I don't feel quite so bad. – Dio Apr 15 '12 at 19:43
  • Explicitly disabling proxy creation on the context did the trick for me. E.g., context.Configuration.ProxyCreationEnabled = false; Hope it helps! – blins Apr 16 '12 at 18:23
  • it is among the possible workaround listed in the link I provided...you can use it as a workaround...but it is not acceptable in general. It is preferrable to avoid using the DTL class in your IQueryable doing a Select and copying its data into a ViewModel version of the entity. I always do this to keep the data layer separated from the presentation Layer. The problem is that the proxy is a subclass and the serializer...doesn't serialize subclasses if they are not declared as known types – Francesco Abbruzzese Apr 16 '12 at 20:35