使用 EntityFramework 4.1 通过 WCF 保存子实体
假设我有一个 Person 和一个 Address 对象,其中 Person 有一个 Address 对象列表。该信息通过 WCF 服务公开,我正在尝试使用 EntityFramework 4.1。
如果我想更新 Person 上的 Address 对象,我想要一个名为 UpdateAddress(Address addr) 的 WCF 方法。
DBContext似乎只有一个Person列表,其中Person对象中有一个Address列表。这是更新地址的唯一方法吗,因为它似乎意味着它需要加载所有人员才能在保存之前找到要更新的地址,从而使其效率低下。
有没有办法只更新而不加载父实体?
Say I have a Person and an Address object where a Person has a list of Address objects. The info is exposed via WCF services, and I'm trying to use EntityFramework 4.1.
If I want to update an Address object on a Person, I want to have a WCF method called UpdateAddress(Address addr).
The DBContext only seems to have a list of Person of which there is a list of Address in the Person object. Is that the only way to update an Address as it seems that implies it needs to load all of the Person to find an Address to update before saving making it inefficient.
Is there a way to just update without loading the parent entity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
Person
是模型的一部分,并且Person
有一个Address
集合,那么还有Address
(作为相关实体) 是模型的一部分(除非您从模型中明确排除了 Address 集合)。这意味着您可以简单地将地址集作为辅助属性添加到 DbContext:对于
UpdateAdress
,您可以使用:即使您不想或无法添加
Addresses
code> DbSet 到上下文,您可以使用上下文的Set()
方法(只要 T 是模型实体 - 并且Address
应该是一个) :If
Person
is part of the model andPerson
has a collection ofAddress
then alsoAddress
(as a related entity) is part of the model (unless you excluded the Address collection explicitely from the model). That means you can simply add the Address set to the DbContext as a helper property:For
UpdateAdress
you can then use:Even if you don't want or can't add an
Addresses
DbSet to the context you can use theSet<T>()
method of the context (as long as T is a model entity - andAddress
should be one):