I’m using MVP pattern and the EF in my C# application. In my database design there is a one-to-many relationship between ‘personas’ and ‘referencias’, each ‘personas’ can have 0 or mutiple ‘referencias’.
According to MVP pattern I have a ‘Personas’ Model which perfoms CRUD operations in my physical database. I have a method which performs insertion like this:
public void AgregaPersona(_Persona persona)
{
Persona per = new Persona()
{
Nombres = persona.nombres,
ApellidoP = persona.apellidoP,
ApellidoM = persona.apellidoM,
FechaNacimiento = persona.fechaNacimiento,
Sexo = persona.sexo.ToString(),
EdoCivil = persona.edoCivil,
RFC = persona.RFC,
CURP = persona.CURP,
Domicilio = persona.domicilio,
CP = persona.codigoPostal,
Telefonos = persona.telefonos,
Celular = persona.celular,
Email = persona.email,
IDDel = persona.idDelegacion,
IDEmpresa = persona.idEmpresa
};
context.personas.AddObject(per);
context.SaveChanges();
}
The question is: how do I relate ‘referencias’ insertion in my code? Following MVP rules I must create a Model for ‘referencias’, isn’t it? Should I call an insertion method defined in ‘referencias’ model?
Here is the code, then I will explain 🙂
Your code to make these work together:
This is all you need to do as far as creating two separate objects (as you expected) that are related to each other. Here is my best description of what is going on here
When you create the
ICollection<Referencia> Referencias, all this is doing is creating a link (relationship) between the two objects (PersonaandReferencia). The objects are still separate, only being linked via this collection.When you go to actually create the
PersonawithReferenciamappings, you have to create yourPersona, then you create the separate object of theReferenciaand relate it to thePersonaby adding it toPersona‘s ICollection mapping (Referencias). When the actual code is run to persist this to the database, it will treat this as separate inserts, something like this pseudo-code:Now, keep in mind the note I made about lazy loading. By default, whenever you load
Persona, it will not load theReferenciasuntil you actually need it. It will only load these objects from the database if you try to access values within this property. Thus, emphasizing even further that these are, indeed two separate objects.Also, you can create a two-way mapping if you want. You simply add another link (relationship), this time from
Referenciato its correspondingPersona.Do take note that the property name is the same as the class name. This is a convention, and if you deviate by naming the property something else, then you will need to add an attribute above your
Referenciasobject inPersona. This is so that EF knows that this is a two-way relationship. So, if you decide to name thePersonaproperty inReferenciasomething likePersonaRef, then your code would look more like this:Hopefully, that gives you a better understanding of how relationships work in EF (and this translates fairly well to other ORMs). So, you get the two distinct models that you need, and can traverse between the two with relationship mapping properties.
Here is a very good article on EF Code First by Scott Gu that you might find helpful