实体框架数据库更新

时间:2020-03-26 15:58:51

标签: c# asp.net-mvc entity-framework

例外:

  HResult=0x80131501
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at SQDCDashboard.Services.Services.WindowsOrderImporterService.ImportOrder(DateTime date) in C:\Users\ME\source\repos\SQDC Dashboard\SQDCDashboard\SQDCDashboard.Services\Services\WindowsOrderImporterService.cs:line 145
   at SQDCDashboardOrderImporterTester.Program.Main(String[] args) in C:\Users\ME\source\repos\SQDC Dashboard\SQDCDashboard\SQDCDashboardOrderImporterTester\Program.cs:line 11

Inner Exception 1:
UpdateException: An error occurred while updating the entries. See the inner exception for details.

Inner Exception 2:
SqlException: Violation of PRIMARY KEY constraint 'PK_dbo.ProductionLines'. Cannot insert duplicate key in object 'dbo.ProductionLines'. The duplicate key value is (ceae2692-ed1d-4902-8e16-781aba577130).
The statement has been terminated.

代码:

    {
       Order ord = new Order
       {
          //ID and CreatedAt are being auto generated
          SerialNumber = serialNum,
          UnitNumber = unitNum,
          ProductionNumber = prodNum,
          MaterialNumber = materialNum,
          SalesNumber = salesNum,
          Location = Core.Enums.Location.Start,
          ProductionLineId = prodLineID, //Foreign Key
          ProdLine = prodLines.Where(x => x.Id == prodLineID).FirstOrDefault()
      };
      context.Orders.Add(ord);
      context.SaveChanges();
   } 

我正在尝试更新数据库以向其添加新订单。每个订单都链接到生产线,并包含其ID,然后是生产线。我相信这是正在发生的事情,是它正在尝试将其添加到ProductionLine表中,但只应添加Orders表。我不确定如何仅编辑Orders表。

型号:

public class Order : BaseEntity
    {
        public string ProductionNumber { get; set; }
        public string SalesNumber { get; set; }
        public string MaterialNumber { get; set; }
        public string SerialNumber { get; set; }
        public string UnitNumber { get; set; }
        public DateTime? CompletionDate { get; set; }
        public Location Location { get; set; }
        public string ProductionLineId { get; set; }
        public ProductionLine ProdLine { get; set; }
    }
public class ProductionLine : BaseEntity
    {
        public string Name { get; set; }
        public double UPE { get; set; }
        public string ComputerName { get; set; }
        public bool ActiveLine { get; set; }
    }

解决方案:

我没有将在其他using语句中查询到的ProductionLine列表设置为不变。将其设置为不变会使EF知道它不需要更新,这意味着它没有尝试重复ID。

2 个答案:

答案 0 :(得分:1)

实体框架插入具有添加状态的每个记录。因此,您的实体也由于插入而具有相同的状态。

您可以使用context.Entry(prodLines.Where(x => x.Id == prodLineID).FirstOrDefault())。State = EntityState.Unchanged;

未更改:实体正在被上下文跟踪并且存在于数据库中,并且其属性值与数据库中的值没有变化

您可以在Entity States and SaveChanges

上查看有关此行为的更多信息。

答案 1 :(得分:0)

您确定主键设置正确吗?

public int OrderId { get; set; }添加到订单中
public string ProductionLineId { get; set; }到ProductionLine

我可以插入一条生产线,并从多个订单中引用它。

您正在使用prodLines.Where(x => x.Id == prodLineID),该属性使用Id属性,该属性不会显示在Model类中。

好像您在BaseEntity中拥有Id属性?