I am trying to get identifiying relationships to work in Entity Framework so I can remove items from their collection using Remove.
I have created a test like:
public class OrderLine{
[Key, Column(Order = 0)]
public int OrderLineId { get; set; }
[Key, ForeignKey("Order"), Column(Order = 1)]
public int OrderId{ get; set; }
public virtual Order Order{ get; set; }
[ForeignKey("Product")]
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int Number { get; set; }}
When I attempt to add a OrderLine like:
var order=_orderRepository.Find(1);
var NewOrderLine= new OrderLine()
{
Number = 1,ProductId= 2
};
order.OrderLines.Add(NewOrderLine);
_orderRepository.InsertOrUpdate(order);
_orderRepository.Save();
I get the error
Cannot insert explicit value for identity column in table 'OrderLines' when IDENTITY_INSERT is set to OFF.
When I look at the table SQL has a primary key over OrderId and OrderLineId. Also only OrderLineId is set as identity. So it must be trying to insert a value but the only value it seems to be trying to insert is 0.
Any ideas I can do an insert to the table using:
INSERT INTO [OrderLines]
([ProductId ]
,[Number]
,[OrderId])
VALUES
(2
,1
,1)