locked
One Virtual Entity in One Class Gets Added to Another Class RRS feed

  • Question

  • I have two classes:

    (1) Product Class (for the Product table in the database):

    public class Product {

            public int ProductID { get; set; }
    ......
    ......
            public string Category { get; set; }

        }

    (2) Order Class (for the Order table in the database):

        public class Order {
            public int OrderId { get; set; }
    ......
            public bool Dispatched { get; set; }

            public virtual List<OrderLine> OrderLines { get; set; }
        }

    When I added the .edmx file, OrderLines was added automatically to not only the Order class, but also the Product class:

    (1.1) The generated Product Class after I added the .edmx file:               

        public partial class Product
        {
            public Product()
            {
                this.OrderLines = new HashSet<OrderLine>();
            }

            public int ProductID { get; set; }
    ......
    ......
    public virtual ICollection<OrderLine> OrderLines { get; set; }
        }

    (2.1) The generated Order Class after I added the .edmx file:

        public partial class Order
        {
            public Order()
            {
                this.OrderLines = new HashSet<OrderLine>();
            }
        
            public int OrderId { get; set; }
      ......
      ......
        
            public virtual ICollection<OrderLine> OrderLines { get; set; }
        }

    OrderLine contains two foreign key: the keys to the Product and Order class instances. Within the Order class, it just makes sure whatever Product is referenced, the Product instance actually exists. But I don't understand why OrderLines should be in the Product class also. The Product class is not cross referencing the Order class.

    I would not mind Orderlines being added to the Product class if my program still worked. It stops working.

    In fact, before adding the .edmx file, my program had worked fine. But I needed the additional functionalities of Entity Framework, so I added the .edmx file.

    My questions:

    (1) Why OrderLines is added to the Product class ?

    (2) My current code does not touch Product.OrderLines (the one added when .edmx file was added). What my code should do with Product.OrderLines ?

    Thanks so much.


    Goldstar Lee



    • Edited by Goldstar Lee Saturday, June 4, 2016 8:40 PM Add clarity
    Saturday, June 4, 2016 7:40 PM

Answers

  • (1) Why OrderLines is added to the Product class ?

    If there is a relationship on the model between the two, then delete the relationship line between the two off of the model diagram.

    • Marked as answer by Goldstar Lee Sunday, June 5, 2016 11:46 PM
    Sunday, June 5, 2016 9:22 PM

All replies

  • (1) Why OrderLines is added to the Product class ?

    If there is a relationship on the model between the two, then delete the relationship line between the two off of the model diagram.

    • Marked as answer by Goldstar Lee Sunday, June 5, 2016 11:46 PM
    Sunday, June 5, 2016 9:22 PM
  • I suppose you meant "If there is NO relationship on the model", instead of "If there is a relationship on the model".

    I took it as so and deleted "OrderLines" from the "Product" within the .edmx file, and it works.

    Great ! My kudo and thanks.


    Goldstar Lee

    Sunday, June 5, 2016 11:46 PM