简体   繁体   中英

How to update Entity through WCF service

I got Entity Framework DbContext to work locally, connecting directly to the SQL database.

Now I am trying to put the Entity Framework stuff behind a WCF Service. The WCF Service will exposes its own set of service layer objects (DataContracts) corresponding to the underlying Entities. The client never knows about the underlying data store and entities.

  • Getting data is easy, just a LINQ to SQL and .ToList();
  • Inserting new objects is not too bad, pass a DataContract from the client to the WCF service via its OperationContract then WCF will call DbContext.Add(), DbContext.SaveChanges();

But how do I do an update? Do I need to preserve the PK? If so, what is the usual way of doing it?

You help is appreaciated. Thanks.

Yes, you need to keep the primary keys around. I put them in the client facing objects themselves. You can pass them back as part of the object when the client is retrieving the data so that they can in turn pass it back to you on an update.

If the PK name doesn't really fit your abstraction, just call it something else, your services just need to know the mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM