Entity Framework 4 POCO Context Persistance Accross Services
I have a WCF service that uses the entity framework to retrieve records from the database and map them to POCO objects. These POCO objects are then returned to the client application.
Lets say the client application changes some properties on one of these POCO objects then sends it back to the WCF service to be saved. As far as I can see this POCO object is then completely disconnected from the entity framework context. The way I currently save this is to create a new context object and retrieve the POCO object from the database again, I then copy property values of the POCO object passed in to the method in to the POCO object just retrieve from the database then call save on t开发者_高级运维he context. Is this the best way to be doing this or is there a simpler method?
The problem here is that EF keeps track of attached objects by using it's own 'unique identifier' for each retrieved object, different from the record's primary key in your database. That property is of course not marked with the DataMember attribute, so when sent over the WCF service you lose the key that was used by EF to keep track of it. When deserializing the object, it's no longer the same exact object, but another simpler one containing only the properties marked with the DataMember attribute. So when the service receives them back again, you of course lose the connection between the object and EF.
Have you tried reattaching the object to the context? I think it should work, but i haven't tried it myself.
精彩评论