I have an DB Entity(Employee) generated through database using Entity Framework in the form of EDMX. I want to use that DB Entity (Employee) as DataContract for my WCF Service. Please help me in providing best option for implementing the same. The attributes of DataContract and DataMember are needed for the Entity Framework to use these objects. Notice on the product object I have a virtual list of sale objects, making this virtual allows for entity framework to use lazy loading. How to add Serializable attributes in all entities generated by Entity Framework 5.0. Ask Question 10. I would be very surprised if the EF template did not already have the option to output attributes for DataContractSerializer (specifically, [DataContract]/. If you use entity framework 5.0 or above Add [Serializable] tag between this.
I am attempting to use EF with Code First and the Web API. I don't have any problems until I get into serializing Many-to-Many relationships. When I attempt to execute the following web api method below I get the following error message:
I get the following error:
'System.Data.Entity.DynamicProxies.Tag_FF17EDDE6893000F7672649A39962DB0CA591C699DDB73E8C2A56203ED7C7B6D' with data contract name 'Tag_FF17EDDE6893000F7672649A39962DB0CA591C699DDB73E8C2A56203ED7C7B6D:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
I have read some SO articles (article 1, article 2) that the fix is to add the following attribute:
[DataContract (IsReference=true)]
but this has had no effect. Also using [IgnoreDataMember] does not have an effect. The only option that does seem to work is to set Configuration.ProxyCreationEnabled to false. Is this my only option? Am I missing something?
Sample POCO objects:
Tag
Blog
2 Answers
When you see an object like:
System.Data.Entity.DynamicProxies.Tag_FF17EDDE6893000F7672649A39962DB0CA591C699DDB73E8C2A56203ED7C7B6D
It is a runtime EF Generated version of a proxy to what would normally be considered a POCO object.
Entity Framework has created this object because it tracks when the objects has changed so when you call .SaveChanges()
it can optimize what to do. The downfall of this is that you aren't actually using the specific object you defined, thus Data Contracts and Frameworks (Json.net) cannot use them as they would your original POCO object.
To Prevent EF from returning this object you have two choices (ATM):
First, Try turning off Proxy object creation on your DbContext.
This will completely disable the create of Proxy objects for every query to the specific DbContext. (This does not affect the cached object in the ObjectContext).
Secondly, use EntityFramework 5.0+ with AsNoTracking()(ProxyCreationEnabled is still available in EF 5.0 as well)
You should also be able to
or
Instead of globally disabling proxy creation for the DbContext, this only turns it off per query. (This DOES affect the cached object in the ObjectContext, it is not cached)
I wanted to leave proxy creation inteact, and found that using the ProxyDataContractResolver
seemed to resolve the issue for me. See msdn for a reference on how to use it in wcf, which isn't exactly WebAPI, but should get you going along the right path.
Not the answer you're looking for? Browse other questions tagged c#entity-framework.net-4.5 or ask your own question.
Re: DataAnnotations with EntityFramework (Database First) method
Aug 24, 2011 04:29 PM|wsyeager36|LINK
Alan, I've already viewed that article. It is not set up for the Database First method. It is set up for Code First. The way I have my Model class set up is supposed to be the way to set it up using EntityFramewowrk with the DataBase First methodology. Take a look at the previous post to see how the class is set up.
Additionally, the content in there is outdated as far as the DLLs go. Check the DLLs I'm using in the above post.
The bottom line is this....
My data model for the Email field specifies the following:
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
The ModelState.Count parameter is correct and the ModelState.Keys are correct along with their respective values that I am checking during debugging in the collection. It's not setting the ModelState.IsValid property to FALSE based on the above directives.
The Model directives above are not being triggered on the front end for validation.
It's amazing how I can't get a simple direct answer of how to use DataAnnotations using the DataBase First methodology...
Entity Framework Connection String
How can this be resolved?