none
EF 5.0 Required Properties for Parent Child Relationship RRS feed

  • Question

  • I been learning EF for some time now and want to make sure I'm doing it correctly.  I've set up the excellent example found at http://msdn.microsoft.com/en-us/data/jj682076.aspx.  The sample creates a Category > Products relationship with the following code first classes.

    public class Category
        {
            private readonly ObservableListSource<Product> _products =
                    new ObservableListSource<Product>();
     
            public int CategoryId { get; set; }
            public string Name { get; set; }
            public virtual ObservableListSource<Product> Products { get { return _products; } }
        }

    public class Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
     
            public int CategoryId { get; set; }
            public virtual Category Category { get; set; }
        }

    This all works great but I noticed that you don't need the properties:

    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }

    for it to work.  Is this because EF is smart enough to figure it out anyway.  If so, why include them?  Thanks!


    Donald Hofferber

    Tuesday, January 8, 2013 1:25 AM

Answers

  • Hi Donald; You state the following:

    This all works great but I noticed that you don't need the properties:

    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }

    As long as you are querying the Category table to get to the product you are correct. But removing those objects from the Product table means that if you wanted to go the other way you don't have a direct way to fiind a Product's Category because you have removed the navigation property and foreign key.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by DDHSolutions Tuesday, January 8, 2013 8:54 PM
    Tuesday, January 8, 2013 4:55 AM

All replies

  • Hi Donald; You state the following:

    This all works great but I noticed that you don't need the properties:

    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }

    As long as you are querying the Category table to get to the product you are correct. But removing those objects from the Product table means that if you wanted to go the other way you don't have a direct way to fiind a Product's Category because you have removed the navigation property and foreign key.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by DDHSolutions Tuesday, January 8, 2013 8:54 PM
    Tuesday, January 8, 2013 4:55 AM
  • Thanks for the clarification Fernando.  I tried all of the possible combinations and now see the differences in the db.

    Donald Hofferber

    Tuesday, January 8, 2013 8:56 PM
  •   

    Not a problem Donald, glad to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, January 9, 2013 4:11 AM