none
Relationships between Entities and ID Properties RRS feed

  • Question

  • Hi :)

    Im using model first and have a question about relationships between two entities.

    Lets say i have two entities.
    Order and OrderDetails. Both of these entities have a ID properties. Now i make a association between the two entities.
    Then i got the navigation properties on the entities.

    Now to the question. Do i need to make a property on the Order entity with the OrderDetailID, or is the association enough?
    Because i can navigate between the entities, but i seen people put the ID property on entities, and i cant figure out if thats nessecery?

    Sincerly
    Jakob

    Saturday, April 26, 2014 8:14 AM

Answers

  • Im aware that there is a OrderId in the database when I make the association, but is it nesserary to expose the OrderId property in my code as a attribute?

    No,  it is not necessary to expose the foreign-key property of a parent object the child object is linked to with foreign-key property exposed in the child object.  EF will take care it by itself the population of parent object's ID to its child object and its foreign-key property, which is normally hidden and you can't address the property. The adding of a new parent object that has a collection of new children objects, EF will make that association and foreign-key properties in objects are set correctly.  

    However, with VS2010 using EF 4 I believe, you can expose all foreign-key properties, in an EF entity/object and I do it using that Database First approach.  

    I have the control, because I am using DTO(s) that represent the entities.

    So I have a new parent DTO that I am going to map to an EF entity parent object and persist it to the database. Once the EF object is saved, the primary-key of the record just inserted is populated back to the EF entity. I can trap and save the ID of the parent object, and populate it to children objects as I am inserting the children objects.

    That's just the way I like to do it -- old school.

    Or I can query for all children objects from the database linked to the parent without having to include the parent in the query to get to the children in a retuned query result.

    Those are the reasons, I expose the foreign-key properties on all child objects on the model, which is a setting that can be selected when the model is first created with the EF model wizard.

        

    Monday, April 28, 2014 11:35 AM

All replies

  • OrderDetail is a child to an Order record. The OrderDetail needs an OrderId column that points back to its parent Order record. Maybe you should look at how constrains are created from a database management solution perspective like MS SQL Server Management Studio or T-SQL script run by db management solution.  You should look at it from DB management solution perspective and not EF.
    Sunday, April 27, 2014 7:38 AM
  • Hi Fred

    Thanks for the respons.

    Im aware that there is a OrderId in the database when I make the association, but is it nesserary to expose the OrderId property in my code as a attribute?

    Monday, April 28, 2014 6:55 AM
  • Im aware that there is a OrderId in the database when I make the association, but is it nesserary to expose the OrderId property in my code as a attribute?

    No,  it is not necessary to expose the foreign-key property of a parent object the child object is linked to with foreign-key property exposed in the child object.  EF will take care it by itself the population of parent object's ID to its child object and its foreign-key property, which is normally hidden and you can't address the property. The adding of a new parent object that has a collection of new children objects, EF will make that association and foreign-key properties in objects are set correctly.  

    However, with VS2010 using EF 4 I believe, you can expose all foreign-key properties, in an EF entity/object and I do it using that Database First approach.  

    I have the control, because I am using DTO(s) that represent the entities.

    So I have a new parent DTO that I am going to map to an EF entity parent object and persist it to the database. Once the EF object is saved, the primary-key of the record just inserted is populated back to the EF entity. I can trap and save the ID of the parent object, and populate it to children objects as I am inserting the children objects.

    That's just the way I like to do it -- old school.

    Or I can query for all children objects from the database linked to the parent without having to include the parent in the query to get to the children in a retuned query result.

    Those are the reasons, I expose the foreign-key properties on all child objects on the model, which is a setting that can be selected when the model is first created with the EF model wizard.

        

    Monday, April 28, 2014 11:35 AM