LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error

后端 未结 2 1398
温柔的废话
温柔的废话 2020-12-18 21:56

This error is being generated when I try to change a foreign key. I know this is a very common error I’ve found tons of information about it and tried to implement the fixe

2条回答
  •  自闭症患者
    2020-12-18 22:14

    I think you are trying to assign an ID when you need to assign the entity. (I'm not 100% sure this is what you are doing)

    // Don't assign just the id
    Person.ParentId = parentId;
    
    // Assign the entity
    Person.Parent = db.Parents.Single(x.Id == parentId);
    

提交回复
热议问题