I use Code First to automatically create my database, and it works fine by creating a table Ordersand a table OrderLines, as expected when I add some test data.
I have the following class Order:
public class Order
{
public int OrderID { get; set; }
public void AddItem(string productCode, int quantity)
{
var existingLine = OrderLines.FirstOrDefault(x => x.ProductOption.ProductCode == item.ProductCode);
if (existingLine == null)
OrderLines.Add(new OrderLine { ProductOption = item, Quantity = quantity });
else
existingLine.Quantity += quantity;
}
public void RemoveItem(string productCode)
{
OrderLines.Remove(OrderLines.Where(x => x.ProductOption.ProductCode == productCode).FirstOrDefault());
}
public virtual ICollection<OrderLine> OrderLines { get; set; }
public Order()
{
OrderLines = new List<OrderLine>();
}
}
I really want to encapsulate the collection OrderLines, which makes it impossible for class consumers to directly add and remove elements to / from it (using the Add/ RemoveICollection methods ), and instead force them to use my own methods AddItemand RemoveItem.
, , EF , EF OrderLines table/foreign.
, , , internal , , OrderLines.
, , - ? !
, , ; . , , , , -, - - ?