I am working on a corporate application that will consist of a rich WPF client that communicates with a bunch of web services to retrieve data. These data are POCOs created using Code First EF 4.2.
Now I am facing a conceptual problem that I am trying to wrap around, but could not find a good solution.
1: n Association
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 that takes care of binding the newly created dtos (including new items in Children Collection) to the Datacontext from the client side and saving it. This, of course, only works if these objects are created on the client side and then sent for addition. The service adds new objects and repeatedly returns the updated objects (Id property) basically.
[ServiceContract] public interface IMyPersonCaretaker { [OperationContract] Person CreatePerson(Person entity) }
However, when I get existing entities, I cannot edit any associations (add or remove objects - because they have a fixed size). So now I will need to expand the interface to allow this:
[ServiceContract] public interface IMyPersonCaretaker { [OperationContract] Person CreatePerson(Person entity) [OperationContract] Person AddChild(Person parent, Person child) }
What seems like a clumsy approach to me, and the interfaces are getting bigger and bigger pretty fast. Is this a complex approach to working with POCOs? How do you do this?
n: m Associations using manual matching
Another 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 through the binding, I get an error message when I try to add it to Set in context. The error says that it is not allowed to remove elements from the Mappings property, and I really donβt understand where this comes from.
The second part may be a bit abstract for the description, if someone needs additional information, I am more than ready to add it!
PS: Please do not offer Selftracking organizations, I know about them, but I'm really interested in a solution based solely on EF 4.2 POCOs.
PPS: the code is written manually directly in this window, and not the actual code used, so maybe something is missing, but this is not a question of my question, so I hope this is enough.