I am not able to use Entities generated by Entity Framework directly in ASP.NET MVC Web-API.
I get error "IsReferenced=true" on class and cannot serialize.
Is there a way to EF in a disconnected way so that entites are generated without "IsReferenced" attribute and can be used directly in Web API for return values as objects.
Ideally don't want to create light weight C# class/entities just to avoid this issue.
I am little confused... it is easy to read things out of context and get different meaning...
Can someone with ASP.NET MVC WEB API team point me to how I get EF entities to work with Web API.
Closest solution I see is I create yet another set of light weight entities just for the purpose of API exposure but internally I do the mapping to/from Entity Framework generated entities.
OR is there a option in Entity Framework -to avoid this scenario and use it in disconnected fashion [Which generates entities without "IsReferenced" attribute]?
Is there a way to EF in a disconnected way so that entites are generated without "IsReferenced" attribute and can be used directly in Web API for return values as objects.
Ideally don't want to create light weight C# class/entities just to avoid this issue.
For JSON you want to use JSON.NET, which I understand will be included in the RC of the web-api.
For XML I want to say you want to use the Data Contract Serializer.
3. Add following code to Application_Start in your Global.asax
GlobalConfiguration.Configuration.Formatters.Clear();
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new IsoDateTimeConverter());
GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(serializerSettings));
I tested twice removing above code -I got expected error + adding it back worked great. Hope that helps.
Below
are couple of useful alternatives: [If you don't want to use JSON.NET] But I am seeing enough references that WebAPI RC will have JSON.NET -so why not go that path..
JTBS
Member
34 Points
36 Posts
Cannot use Entities in Entity Framework directly with Web-API.
Apr 18, 2012 02:44 AM|LINK
I am not able to use Entities generated by Entity Framework directly in ASP.NET MVC Web-API.
I get error "IsReferenced=true" on class and cannot serialize.
Is there a way to EF in a disconnected way so that entites are generated without "IsReferenced" attribute and can be used directly in Web API for return values as objects.
Ideally don't want to create light weight C# class/entities just to avoid this issue.
DPeden
Member
14 Points
15 Posts
Re: Cannot use Entities in Entity Framework directly with Web-API.
Apr 18, 2012 03:41 PM|LINK
http://en.wikipedia.org/wiki/Single_responsibility_principle
JTBS
Member
34 Points
36 Posts
Re: Cannot use Entities in Entity Framework directly with Web-API.
Apr 18, 2012 03:53 PM|LINK
I am little confused... it is easy to read things out of context and get different meaning...
Can someone with ASP.NET MVC WEB API team point me to how I get EF entities to work with Web API.
Closest solution I see is I create yet another set of light weight entities just for the purpose of API exposure but internally I do the mapping to/from Entity Framework generated entities.
OR is there a option in Entity Framework -to avoid this scenario and use it in disconnected fashion [Which generates entities without "IsReferenced" attribute]?
Any thoughts/pointers are appreciated
Jay_Harlow
Member
86 Points
26 Posts
Re: Cannot use Entities in Entity Framework directly with Web-API.
Apr 18, 2012 04:13 PM|LINK
For JSON you want to use JSON.NET, which I understand will be included in the RC of the web-api.
For XML I want to say you want to use the Data Contract Serializer.
I don't have my web-api project handy, so I'm going from memory...
Hope this helps
Jay
JTBS
Member
34 Points
36 Posts
Re: Cannot use Entities in Entity Framework directly with Web-API.
Apr 25, 2012 12:27 AM|LINK
Thanks for the response.
I adde JSON.NET ... to help everyone following are detaisl:
1. Use Nuget to install JSON.NET and add to your web-api project
2. Add JsonNetFormatter from: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
3. Add following code to Application_Start in your Global.asax
I tested twice removing above code -I got expected error + adding it back worked great. Hope that helps.
Below are couple of useful alternatives: [If you don't want to use JSON.NET] But I am seeing enough references that WebAPI RC will have JSON.NET -so why not go that path..
http://www.strathweb.com/2012/03/serializing-entity-framework-objects-to-json-in-asp-net-web-api/