locked
Inserting Many to Many RRS feed

  • Question

  • Considering my database model:

    GlobalCorporations
    (GlobalCorporationID, GroupName, ...)

    Address
    (AddressID, GlobalCorporationID, AdressLine1, AdressLine2, ...)

    Companies
    (CompanyID, GlobalCorporationID, CompanyName, ...)

    CompaniesAdresses
    (CompanyID, AddressID, AdressType, RowGuid)

    After mapping using EF4, i would wanna know if the best way to insert an Address of an existing Company and GlobalCorporation is:


    //Get an existing Global Corporation.

     

     


    GlobalCorporations
    objGlobalCorporation = (
    GlobalCorporations)_Context.GetObjectByKey(new EntityKey("MyEntities.GlobalCorporations", "GlobalCorporationID", SomeKey));

    //Get an existing Company.
    Companies
    objCompany = (Companies)_Context.GetObjectByKey(new EntityKey("MyEntities.Companies", "CompanyID", SomeKey));


    //Create a new Address.
    Adresses objAddress = new Adresses();
    //Set some adderess values.
    (...)

    //Create a new Company Address. Is it necessary ?
    CompaniesAdresses objCompanyAddress = new CompaniesAdresses();

    //From here to final, I don´t know the best way to relation those objects.

    Example 1 (This way caused AddressID and CompanyID error):
    objAdress.CompanyAddresses.Add(objCompanyAddress);


    Example 2 (This way works but it's really the best way ?):
    objCompanyAddresses.CompanyID = objCompany.CompanyID;
    objCompanyAddresses.AddressID = objAddress.AddressID;
    objAdress.CompanyAddresses.Add(objCompanyAddress);

    //And now, is just relation the GlobalCorporation with this others ???

    Example:

    objGlobalCorporation.Addresses.Add(objAdress);


    _Context.SaveChanges();


    Thanks a lot,
    Rubens Cury

    Wednesday, January 27, 2010 5:02 AM

Answers

  • Judging by the date of the post I'd say the post was written using EFv1 before foreign keys existed in the conceptual model.
    • Marked as answer by Rubens Cury Saturday, January 30, 2010 3:55 PM
    Saturday, January 30, 2010 1:20 AM

All replies

  • Hi Rubens,

    In my opinion Example 2 is the best way to go.  The use of foreign keys is a great enhancement in EF4.
    Did you have a question other than if that is a good way to do that?

    Mike
    Wednesday, January 27, 2010 1:05 PM
  • Hi Mike,

    Actually, my question is about the best way to make the right relations with objects before insert.

    For example:

    objCompanyAddresses.CompanyID = objCompany.CompanyID;
    objCompanyAddresses.AddressID = objAddress.AddressID;

    objAdress.CompanyAddresses.Add(objCompanyAddress);

    OR

    objCompanyAddresses.Companies = objCompany;
    objCompanyAddresses.Addresses = objAddress;

    objAdress.CompanyAddresses.Add(objCompanyAddress);


    Thanks.
    Wednesday, January 27, 2010 6:29 PM
  • My personal preference is the first option.  I'd be interested to find out if they create different sql.
    Thursday, January 28, 2010 2:18 PM
  • Hi,

    First all, thanks for your post. What´s your preference is what i´m doing but...

    I´m still little amateur EF user, and i´ve noticed the most of times we have a possibility to create a objects relation  setting its FK ID or THE OBJECTS EACH OTHER.

    In this example posted by Julia Lerman, the best way to relation two objects is assign each other instead of the FK IDs.

    http://thedatafarm.com/blog/data-access/inserting-many-to-many-relationships-in-ef-with-or-without-a-join-entity/

    Sorry if my question does not make much sense, but i´m a little confused about it.

    Thanks.

    Friday, January 29, 2010 9:05 PM
  • Judging by the date of the post I'd say the post was written using EFv1 before foreign keys existed in the conceptual model.
    • Marked as answer by Rubens Cury Saturday, January 30, 2010 3:55 PM
    Saturday, January 30, 2010 1:20 AM