有几个实体:操作、批次和销售价格。我想添加一个新的操作,新的批次和新的价格。
Batch newBatch = new Batch
{
Product = product,
Product_Code = product.Code,
Purchase = Helper.ConvertAndRound(Purchase.Text),
Quantity = Helper.ConvertAndRound(Quantity.Text),
Create_Time = now
};
Sale_Price sale_Price = new Sale_Price
{
Sale = Helper.ConvertAndRound(Sale.Text),
Product = product,
Product_Code = product.Code,
Start_Date = now,
End_Date = null
};
operation = new Operation
{
Batch = newBatch,
Sale_Price = sale_Price,
Quantity = Helper.ConvertAndRound(Quantity.Text),
Discount = 0
};
添加到数据库:
myContext.Operations.AddRange(operations);
myContext.SaveChanges();
添加了所有实体,但未建立链接(操作 Batch_id = null 和 sale_price = null)。设置链接似乎是正确的,因为如果数据库中有链接,那么它会加载它们。
如果你用拐杖放下身份证,那么一切正常:
myContext.Operations.AddRange(operations);
myContext.SaveChanges();
foreach (Operation operation in operations)
{
operation.Batch_Id = operation.Batch.Id;
operation.Sale_Price_Id = operation.Sale_Price.Id;
}
myContext.SaveChanges();