简体   繁体   English

如何通过WCF服务更新实体

[英]How to update Entity through WCF service

I got Entity Framework DbContext to work locally, connecting directly to the SQL database. 我让Entity Framework DbContext在本地工作,直接连接到SQL数据库。

Now I am trying to put the Entity Framework stuff behind a WCF Service. 现在,我试图将实体框架的内容放在WCF服务之后。 The WCF Service will exposes its own set of service layer objects (DataContracts) corresponding to the underlying Entities. WCF服务将公开与基础实体相对应的自己的一组服务层对象(DataContracts)。 The client never knows about the underlying data store and entities. 客户端永远不知道基础数据存储和实体。

  • Getting data is easy, just a LINQ to SQL and .ToList(); 获取数据很容易,只需使用LINQ to SQL和.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(); 插入新对象还不错,将DataContract从客户端通过其OperationContract从WCF服务传递到WCF服务,然后WCF将调用DbContext.Add(),DbContext.SaveChanges();。

But how do I do an update? 但是,如何进行更新? Do I need to preserve the PK? 我需要保留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. 如果PK名称与您的抽象名称不符,只需将其命名为其他名称,您的服务只需要知道映射即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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