如何在基于 WCF 的场景中管理 EF 4.2 关联
我正在开发一个企业应用程序,该应用程序将包含一个丰富的 WPF 客户端,该客户端与一组 Web 服务通信以检索数据。这些数据是 POCO,使用 Code First EF 4.2 创建。
我现在面临一个概念性问题,我一直在努力解决这个问题,但找不到一个好的解决方案。
1:n 关联
所以数据模型看起来像这样:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Children { get; set; }
}
服务器端我有一个接口,负责将新创建的 dtos(包括 Children Collection 中的新项目)从客户端附加到数据上下文并保存它。当然,只有在客户端创建这些实体然后发送以进行添加时,这才有效。该服务添加新实体并返回更新的实体(主要是 Id 属性)。
[ServiceContract]
public interface IMyPersonCaretaker
{
[OperationContract]
Person CreatePerson(Person entity)
}
但是,当我检索已经存在的实体时,我无法编辑任何关联(添加或删除实体 - 因为它们具有固定大小)。所以现在我需要扩展界面以实现这一点:
[ServiceContract]
public interface IMyPersonCaretaker
{
[OperationContract]
Person CreatePerson(Person entity)
[OperationContract]
Person AddChild(Person parent, Person child)
}
对我来说,这似乎是一种笨拙的方法,而且界面变得越来越大。这是与 POCO 合作的复杂方法吗?你怎么做?
通过手动映射进行 n:m 关联
数据模型的不同部分如下所示:
public class ClassA
{
public int Id { get; set; }
public virtual ICollection<AtoBMapping> Mappings { get; set; }
}
public class ClassB
{
public int Id { get; set; }
public virtual ICollection<AtoBMapping> Mappings { get; set; }
}
public class AtoBMapping
{
public int Id { get; set; }
public virtual ClassA A { get; set; }
public virtual ClassB B { get; set; }
}
每当我尝试在客户端创建 ClassA 和 ClassB 的实例并通过绑定将其添加到彼此时,我当我尝试将其添加到上下文中的集合时出现错误。该错误表明不允许从 Mappings 属性中删除项目,我真的不明白这是从哪里来的。
第二部分的描述可能有点过于抽象,如果有人需要更多信息,我非常愿意添加它!
PS:请不要建议 Selftracking Entities,我了解它们,但我真的对纯粹基于 EF 4.2 POCO 的解决方案感兴趣。
PPS:代码是手工编写到这个窗口中的,而不是实际使用的代码,所以可能会缺少一些东西,但这不是我问题的重点,所以我希望它就足够了。
I am working on an enterprise application which will consist of a rich WPF client which talks to a bunch of webservices to retrieve data. This data are POCOs, created with Code First EF 4.2.
I am now facing a conceptual problem which i have been trying to wrap my head around but couldnt find a good solution to.
1:n Associations
So the datamodel looks like this:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Children { get; set; }
}
Serverside i have an interface which takes care of attaching newly create dtos (including new items in the Children Collection) from client side to the datacontext and saving it. This of course only works if those entities are created on the client side and then sent to be added. The service adds new entities and retursn the updated entities (Id property mainly) back.
[ServiceContract]
public interface IMyPersonCaretaker
{
[OperationContract]
Person CreatePerson(Person entity)
}
However when i am retrieving already existing entities i cannot edit any associations (add or remove entities - because they are of fixed size). So now i would need to expand the interface to allow this:
[ServiceContract]
public interface IMyPersonCaretaker
{
[OperationContract]
Person CreatePerson(Person entity)
[OperationContract]
Person AddChild(Person parent, Person child)
}
That to me seems a clumsy approach and the interfaces are getting bigger and bigger pretty fast. Is this the sophisticated approach of working with POCOs? How do you do it?
n:m Associations via manual Mapping
A different part of the datamodel looks like this:
public class ClassA
{
public int Id { get; set; }
public virtual ICollection<AtoBMapping> Mappings { get; set; }
}
public class ClassB
{
public int Id { get; set; }
public virtual ICollection<AtoBMapping> Mappings { get; set; }
}
public class AtoBMapping
{
public int Id { get; set; }
public virtual ClassA A { get; set; }
public virtual ClassB B { get; set; }
}
Whenever i try to create an instance of ClassA and ClassB on the client side and add it to each other via a binding, i get an error when i try to add it to the Set in the context. The error says that it is not allowed to remove items from the Mappings property, and i dont really understand where this is coming from.
The second part might be a little bit too abstract description-wise, if someone needs further info, i am more than willing to add it!
PS: Please dont suggest Selftracking Entities, i know about them, but i would really be interested in a solution which is based purely on EF 4.2 POCOs.
PPS: The code is written by hand right into this window and not actual used code, so there might be stuff missing, but that is not the point of my question, so i hope it will suffice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个类似的解决方案,我们需要一大堆 CRUD 类型操作,但是我们的数据模型不会离开服务器,我们使用自动映射器将对象映射到单独的 DTO,这些 DTO 通常是 WCF DatContract 类,并且没有那么多关系域模型。
最初,这似乎是一个非常冗长的方法,但在我看来,它最终会得到回报,因为您对接口有很多明确的控制,并且在很多情况下,您实际上并不需要将整个域模型(及其所有关系)传输到客户端,一个客户端一般只能显示这么多数据。
另一个选择可能是 WCF DataServices,它们将使用 RESTful 接口来传输数据。
[新选项]
我过去使用的另一个选项基本上只是一种采用字节数组的 CRUD 服务方法。
使用 NetDataContractSerializer 在这些方法之间序列化/反序列化对象图。然后使用来回传输数据的自定义客户端,创建对象并将它们附加到 DataContext 来执行操作......
像这个
I have a similar solution where we need a whole bunch of CRUD type operations, but our datamodel does not leave the server, we map the objects to separate DTOs using automapper, these DTOs are generally the WCF DatContract classes and dont have as many relationships as a domain model.
Initially this might seem like a very verbose apprach, but IMO it eventually pays off because you have much explicit control over your interfaces, and in a lot of cases you dont really need to transfer the whole domain model (with all its relationships) to the client, a client can generally only display so much data.
another option might be WCF DataServices, they would use RESTful interfaces to transfer your data.
[New Option]
one other option i used in the past basically just one CRUD service methods that takes a byte array.
Use NetDataContractSerializer to serialize/deserialize object graphs to and from those methods. Then use a custom client that tranfers data back and forth, create objects and attatch them to the DataContext to do operations....
something like this