A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'

戏子无情 提交于 2019-12-30 07:56:34

问题


I can't figure out why I get this error when I try to add a Venue object and call SaveChanges(). The only difference in the model with Venue objects is they are 1 to 1..0 relation with City.

City city = ProcessCityCache(ev, country, db); // After this call, 'city' is already persisted.
venue = new Venue {
    TicketMasterURL = ev.VenueSeoLink,
    Name = Capitalize(ev.VenueName),
    City = city
};
db.Venues.AddObject(venue);
db.SaveChanges(); // Exception thrown here.

Any insight would be greatly appreciated!

(Open the image in its own tab/window to see full size)


回答1:


I found the problem. It was my fault. I had my FK_Venue_City relationship set as City.ID -> Venue.ID where what I wanted was City.ID -> Venue.CityID. I made that change in my database then updated the model.




回答2:


OK, adding my experience with this obscure error message:

I had the same exception and after comparing the database against the EDMX (what a tedious task!!) I found that I mistakenly set a one-to-one relationship the other way around (foreign was primary and primary was foreign).

Once I fixed that it all worked smoothly!

Anyway - hopefully my lost 3-4 hours will help someone! :)




回答3:


I've been suffering with a similar problem. In my case, I'd set up the FK relationship incorrectly and then imported the table with the incorrect FK into my EF model. Despite fixing the FK relationship with the Update Wizard in Visual Studio my correction wasn't picked up.

To solve this, I deleted the table from the EF model and then re-imported it using the Update Wizard which finally picked up in the change. I hope this helps someone else!




回答4:


My case was also an incorrectly configured association. Specifically, I had set up a 1..1 relationship when what I needed was a 1..0,1




回答5:


Adding my two bits. I had this error and eventually figured out that I had my parent/child relationship reversed. Once I cleaned up that relationship, it worked again.



来源:https://stackoverflow.com/questions/6642566/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!