The property 'Id' is part of the object's key information and cannot be modified

后端 未结 15 2298
天涯浪人
天涯浪人 2020-11-27 07:02

i\'m using Entity Framework 4.0 and having a silly problem that i can\'t figure out.

I have two tables:

  1. Contact: Id (primary key), Value, ContactTypeId
15条回答
  •  执笔经年
    2020-11-27 07:43

    I had this happening when I was editing related objects from two separate contexts at the same time. Example:

    DataContext ctxA = new DataContext();
    DataContext ctxB = new DataContext();
    
    Author orwell = new Author {Name = "George Orwell" };
    ctxA.Add(orwell);
    ctxB.Add(new Book {Name = "1984", Author = orwell});
    
    ctxA.SaveChanges();
    ctxB.SaveChanges();
    

    My case was a little bit more convoluted (as this is obviously quite stupid) but in essence this was causing the error in my case.

提交回复
热议问题