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.