Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:43:03+00:00 2026-05-31T23:43:03+00:00

I’m using MVP pattern and the EF in my C# application. In my database

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:43:04+00:00Added an answer on May 31, 2026 at 11:43 pm

    Here is the code, then I will explain 🙂

    public class Persona
    {
        public Persona()
        {
            //Make sure that Referencias is instantiated by default
            Referencias = new List<Referencia>();
        }
    
        public String Nombres {get; set;}
        //...The other properties of Persona
        public Int publicIDEmpresa {get; set;}
        //The virtual is here for lazy loading 
        public virtual ICollection<Referencia> Referencias {get; set;} 
    }
    
    public class Referencia
    {
        public Int ReferenciaId {get; set;}
        public String Nombre {get; set;}
        //...Other properties of Referencia
    }
    

    Your code to make these work together:

        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
                };
                Referencia newRef = new Referencia
                {
                    Nombre = referenciaNombre;
                    //Fill the rest of the properties except ID (this should be auto)
                }
                per.Referencias.Add(newRef);
                context.personas.AddObject(per);
                context.SaveChanges();
        }
    

    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 (Persona and Referencia). The objects are still separate, only being linked via this collection.

    When you go to actually create the Persona with Referencia mappings, you have to create your Persona, then you create the separate object of the Referencia and relate it to the Persona by adding it to Persona‘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:

    BEGIN TRANSACTION
    INSERT PERSONA
    GET PERSONA ID
    INSERT REFERENCIA USING NEW PERSONA ID(Repeat until all Referencias are inserted)
    COMMIT TRANSACTION
    

    Now, keep in mind the note I made about lazy loading. By default, whenever you load Persona, it will not load the Referencias until 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 Referencia to its corresponding Persona.

    public class Referencia
    {
        public Int ReferenciaId {get; set;}
        public String Nombre {get; set;}
        //....Other properties of Referencia
        public virtual Persona Persona {get;set;}
    }
    

    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 Referencias object in Persona. This is so that EF knows that this is a two-way relationship. So, if you decide to name the Persona property in Referencia something like PersonaRef, then your code would look more like this:

    public class Persona
    {
        public Persona()
        {
            //Make sure that Referencias is instantiated by default
            Referencias = new List<Referencia>();
        }
    
        public String Nombres {get; set;}
        //...The other properties of Persona
        public Int publicIDEmpresa {get; set;}
        //The virtual is here for lazy loading 
        [InverseProperty("PersonaRef")]
        public virtual ICollection<Referencia> Referencias {get; set;} 
    }
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.