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

26,369

Solution 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.

Solution 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! :)

Solution 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!

Share:
26,369
Nick Strupat
Author by

Nick Strupat

Updated on September 17, 2020

Comments

  • Nick Strupat
    Nick Strupat over 3 years

    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!

    Model Diagram

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