In the following code, I don’t believe I am changing the foreign key, but I could be wrong. When I run it, I get the exception:
ForeignKeyReferenceAlreadyHasValueException
Here is the code:
UsersModule um = dc.UsersModules.Where(x => x.UserId == IdParam).FirstOrDefault();
int oldModuleId = um.ModuleId;
string oldModule = um.Module.ModuleName;
The error is thrown on the line:
string oldModule = um.Module.ModuleName
I don’t know why I am getting this error because I am just trying to store the value of ModuleName into string oldModule, not change um.Module.ModuleName
LINQ works based on relational entities. So, whenever you need to set/read a foreign key value, it must be done by setting/reading the foreign entity instelad. Which means that shortcuts such as “Module.ModuleName” does not work very well.