LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error

后端 未结 2 1399
温柔的废话
温柔的废话 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);
    
    0 讨论(0)
  • 2020-12-18 22:21

    For your second question , you can load all the sub_units by

    var groups = db.sub_units.ToArray() and once in memory do something like this:

    foreach(var ticket in tickets)
    {
        ticket.assigned_to_group = groups.Single(f => f.id == assigned_to);
    }
    
    0 讨论(0)
提交回复
热议问题