问题
I had the below .net Core code working with Microsoft.EntityFrameworkCore.SqlServer 2.2.1.
After I upgrade the dotnet core sdk to 2.2.3 it is not working. Note that the related entity is not mapped with a FK in DB. I have a partial class that has the relation and has the attribute NotMapped to make this work in 2.2.1 . I am not sure what changed and what I need to add to save header and detail record.
var soHeader = new masSoHeader();
var orderToAdd = Mapper.Map(salesOrder.SalesOrderHeader, soHeader);
orderToAdd.InsertDateTime = DateTime.Now;
var newSalesOrder = _context.masSoHeader.Add(orderToAdd);
var newSoSalesOrderDetails =
Mapper.Map<List<soSalesOrderDetail>>(salesOrder.SalesOrderDetails);
if (newSoSalesOrderDetails != null)
{
foreach (var soSalesOrderDetail in newSoSalesOrderDetails)
{
soSalesOrderDetail.InsertDateTime = DateTime.Now;
newSalesOrder.Entity.soSalesOrderDetail.Add(soSalesOrderDetail);
}
}
int numberOfRowsInserted = _context.SaveChanges();
来源:https://stackoverflow.com/questions/55133538/ef-core-2-2-3-child-record-is-not-saved-after-update-from-2-2-1