16

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 fixes I’ve found but I still get this error when trying to update Keys. Reference Thread

Originally I was just directly assigning the value and not trying to map the entities.

ticket.assigned_to_group = assigned_to

I’ve since changed to try and map the entities which I believe the correct answer; however I still get the error.

ticket.assigned_to_group = db.sub_units.Single(f => f.id == assigned_to).id;

Any idea why this would not work. Also if I have a table with multiple foreign keys, do I really need to do a new query for each key or is there a better way?

Community
  • 1
  • 1
Tim
  • 377
  • 7
  • 19

2 Answers2

39

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);
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
  • Ah you are correct I was still trying to assign the ID a new value instead of assigning the entity. This now works: t.sub_unit = db.sub_units.Single(f => f.id == assigned_to) – Tim Feb 24 '10 at 18:39
  • This (in my case) is only an issue when the Id is not nullable, updating nullable ids via assigning only a new id works ok. Why is this the case? – CRice Jun 11 '10 at 01:49
  • You don't to assign the ParentId. The only assignment needed is the entity. – Gabriel GM Jan 17 '12 at 15:52
  • Why is that so? What's the difference between assigning the ID vs assigningen the entity? – Jan Aagaard Mar 23 '15 at 18:06
  • L2S's generated models throw such exception in its getters. Google'd around, couldn't find documentation or remarks on that, so I'd guess it's design choice. It's also actually confusing to change Id manually though, even in EF. Say the original `ParentId` is 4, and `Parent` has been loaded. Your code changes it to 5, then edits `Person.Parent.Name`', then do SaveChanges. Which `Parent`'s `Name` have you changed, ID=4 or ID=5? – Xiang Wei Huang Jun 20 '23 at 06:08
  • p.s. Tested on EF6, where data model properties all have plain `{ get; set; }`, The answer would be ID=4's `Name` got changed. So simply changing FK is okay using either Id or navigation property, but one needs to be careful when they're changing properties after that too. – Xiang Wei Huang Jun 20 '23 at 06:25
0

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);
}
Gregoire
  • 24,219
  • 6
  • 46
  • 73
  • Thats not exatcly what I ment. The ticket class has FK's to several other tables besides the 'sub_units'/assigned_to_group table.key. ticket.priority1 = db.priorities.Single(p => p.id == priority_id); ticket.sub_unit = db.sub_units.Single(f => f.id == assigned_to); Or will I need to do that for each related table. – Tim Feb 24 '10 at 18:51