locked
0..1 relationship to itself??? how to represent it on the EF domain model? RRS feed

  • Question

  • hi all

    Here is a very simple scenario:

    Element Table
    -ElementID
    -Name
    -ParentID (this could be another ElementID - so this is a 0..1 relationship to itself)


    SubElement1 Table
    -ElementID
    -SomeField1


    Question:
    So for the scenario above I am using Table-per-Type and I then added "Inheritance..." by using the EF diagram tool.
    However, is there anything I need to do here for the "ParentID" field? Do I need to create a relationship here?

    Thank you


    Max
    Tuesday, November 15, 2011 12:14 AM

Answers

  • Hi,

    If you use Database first you should in your database have a foreign key relationship between Element.ParentId and Element.Id. When you update your model from the database in the database this should automatically update your entity with the needed parent relation.

    No extra steps is neccessary, no fluent api or anything.


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Maximusdm Wednesday, November 16, 2011 3:49 AM
    Tuesday, November 15, 2011 8:32 PM

All replies

  • Hi Max,

    Welcome!

    I think you can refer this link: http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting-part-ii-adding-inheritance/

    In EF4.1, you can use Fluent API to do TPT: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 15, 2011 9:24 AM
  • hey Allan,

    I actually originally used robbagby before but was wondering what to do with a 0..1 relationship pointing to itself.

    We use the Database first method, not Code first. In that case, is it still okay to use Fluent APIs?

    Thanks


    Max
    Tuesday, November 15, 2011 3:27 PM
  • Hi,

    If you use Database first you should in your database have a foreign key relationship between Element.ParentId and Element.Id. When you update your model from the database in the database this should automatically update your entity with the needed parent relation.

    No extra steps is neccessary, no fluent api or anything.


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Maximusdm Wednesday, November 16, 2011 3:49 AM
    Tuesday, November 15, 2011 8:32 PM
  • hi there

    So I finally found a blog which contains something similar to my problem.
    Bear in mind, we do not use Code First in our company. The DBAs create the schema for us.

    So it's hard to import the schema and start laying out the entities, relationships and hierarchy when EF is totally new to us.
    Anyways, here is an example:

    http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx

    public class Category
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public int? ParentCategoryId { get; set; }
     
        public virtual Category ParentCategory { get; set; } <========================== How do I create this by using the EF designer?
        public virtual ICollection<Category> ChildCategories { get; set; }
        public virtual ICollection<Item> Items { get; set; }
    }
    
    

    As you can see, "ParentCategory" was created ok, but that is First code with fluent APIs and we don't use that.
    We rely on the EDMX file and the EF design tool.

    The question is, how do i create the Parent relationship above? We are using the T4 templates to create the POCO classes
    and our class don't have that line of code created. There is something we are missing in our EF diagram model.

    Thank you

     


    Max
    Tuesday, November 15, 2011 10:45 PM
  • ok I reviewed the link below and it helped me understand better:

    http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx

    no need to do anything as it was mentioned by Rune :-)

     

    thanks all


    Max
    Wednesday, November 16, 2011 3:50 AM