Referencing same Entity with different names in an Entity

asp.net-mvc c# entity-framework

Popular Answer

Because I attached the same entity (class) with the same Pk twice, this exception occurred in my case. E.g.:

db.Users.Attach(dbRecord.CreatorUser);
db.Users.Attach(dbRecord.OwnerUser); //<- If CreatorUser== OwnerUser then ERROR

A straightforward comparison will help you avoid this:

db.Users.Attach(dbRecord.CreatorUser);
if (dbRecord.CreatorUser.Pk != dbRecord.OwnerUser.Pk)
{
    db.Cabinets.Attach(dbRecord.OwnerUser);
}
1
4/9/2014 2:53:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow