Exception:
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.
Code:
{
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();
}
I am trying to update my DB to add new orders to it. Each order is linked to a ProductionLine and contains its ID and then the ProductionLine. I believe was is happening, is that it is trying to add to the ProductionLine table but it should only be adding the the Orders table. I am not sure how to get it to only edit the Orders table.
Models:
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; }
}
Solution:
I did not set my ProductionLine list, which was queried in a different using statement, to unchanged. Setting it to unchanged let EF know that it did not need to update it, which meant it was not trying to duplicate IDs.
Entity Framework inserts every records with Added state. So your entity has same state too which due to being insert.
you can set it's state using context.Entry(prodLines.Where(x => x.Id == prodLineID).FirstOrDefault()).State= EntityState.Unchanged;
Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
You can see more information about this behavior at Entity States and SaveChanges
Are you sure you are setting your primary keys correct?
When adding public int OrderId { get; set; }
to Order
and public string ProductionLineId { get; set; }
to ProductionLine
I can Insert a Production Line and reference it from multiple Orders.
You are using prodLines.Where(x => x.Id == prodLineID)
which uses an Id
property, that doesn't show up in your Model class.
Seems like you have a Id
property in your BaseEntity?
User contributions licensed under CC BY-SA 3.0