Conflicting changes detected. This may happen when trying to insert multiple entities with the same key

c# ef-code-first entity-framework-6 sql-server

Question

I'm trying to have Auto Generated ID with EF6. The table in the Database has Identity set to yes on the PK. I keep getting the error "Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.". I'm using Code First. Model:

 public partial class CurrentEquipmentOrderStatu
{

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public long CurrentEquipmentOrderStatusId { get; set; }

    public long? GPSInsightRowId { get; set; }

    [StringLength(50)]
    public string Status { get; set; }

    [StringLength(50)]
    public string TimeToJob { get; set; }

    public long? OrderEquipAssignmentId { get; set; }

    [StringLength(50)]
    public string DistanceToJob { get; set; }

    public long? EquipmentId { get; set; }

    public virtual Equipment Equipment { get; set; }

    public virtual GPSInsight GPSInsight { get; set; }

    public virtual OrderEquipAssignment OrderEquipAssignment { get; set; }
}

Create and save method:

 public static void CreateCurrentOrderStatus()
    {
        var Status = new List<CurrentEquipmentOrderStatu>();
        using (FFAELDB db = new FFAELDB())
        {
            db.Configuration.AutoDetectChangesEnabled = false;
            var EquipAssignments = db.OrderEquipAssignments;
            foreach(OrderEquipAssignment assign in EquipAssignments)
            {

                 db.CurrentEquipmentOrderStatus.Add(new CurrentEquipmentOrderStatu()
                {
                    EquipmentId = assign.EquipmentId,
                    OrderEquipAssignmentId = assign.OrderEquipAssignmentId,



                });

            }
            db.SaveChanges();

            db.Dispose();
        }

    }

Any Help would be greatly appreciated.

1
1
10/14/2016 5:17:48 PM

Popular Answer

There could be a problem with a foreign key. I had mapped my foreign key wrongly so I was getting this error. By correcting the FK and updating it in my Entity Framework it worked.

3
12/4/2017 9:33:10 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow