MVC SPA w/o EF: You must write an attribute 'type'='object' after writing the attribute with local name '__type'

StackOverflow https://stackoverflow.com/questions/10156347

  •  31-05-2021
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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+

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top