简体   繁体   中英

Entity Framework - entity splitting

Suppose I have the following class structure. When I map it to tables, I would create four tables: 'Order' table with OrderId being primary key and OrderDate being date/time column; 'OrderDetail' with OrderId being primary key and foreign key to 'Order' table ('Description' would be another column of 'OrderDetail' table). 'Address' table with OrderId being primary key and foreign key to 'OrderDetail' table ('Address' table would also contain address1 and city), and finally, 'ExtraStuff' table with OrderId being primary key and foreign key to 'OrderDetail. It's all good so far.

Now for the question. Suppose, I want to remove 'Description' from OrderDetail class. The easiest way to map it would be to just drop 'Description' column from 'OrderDetail' table, but that would leave OrderDetail table with just one OrderId column, which I don't want. How can I use OrderDetail class in my case so that I don't create OrderDetail table with just one column? I want to logically use OrderDetail but have 'Address' and 'ExtraStuff' table use OrderId from 'Order' table.

public class Order
{
    public int OrderId { get; set; }

    public DateTime OrderDate { get; set; }

    public virtual OrderDetail Detail { get; set; }
}


public class OrderDetail
{    
    public OrderDetail()
    {
        this.Addresses = new HashSet<Address>();
        this.Extra = new HashSet<ExtraInfo>();
    }

    public int OrderId { get; set; }

    public string Description { get; set; }

    public virtual ICollection<Address> Addresses { get; private set; }

    public virtual ICollection<ExtraInfo> Extra { get; private set; }
}

public class Address
{    
    public int OrderId { get; set; }

    public string Address1 { get; set; }

    public string City { get; set; }
}

public class ExtraInfo
{    
    public int OrderId { get; set; }

    public string SomeStuff { get; set; }        
}

IMHO, keep it simple. Merge OrderDetail with Order if you don't plan to have any specific properties in OrderDetail and don't want to have such a table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM