16

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)

Community
  • 1
  • 1
Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
  • Exact message and other details of the Exception? I guess it's the title but that's not 100% clear. – H H Jul 10 '11 at 17:25
  • That's correct. The title is the InnerException. The outer is `An error occurred while updating the entries. See the inner exception for details.` – Nick Strupat Jul 10 '11 at 17:28

5 Answers5

30

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.

Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
  • 1
    Had same issue. Easy to screw these sorts of things up with all those Id's floating around. Thanks! – itsmatt Feb 09 '12 at 11:53
  • The exact same problem I had, I did table1.Id -> table2.Id instead of table1.table2Id -> table2.Id. Oh god this is shameful. Thank you so much, god only knows how many hours have you saved people like me – Rickless May 31 '19 at 18:56
8

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

justabuzz
  • 842
  • 1
  • 9
  • 19
  • What a pain...finally found the error. Was my mistake. You give me the right direction.In my case, made an association with two auto generated columns (identity). Hope this will help someone. – Xilmiki Mar 19 '14 at 18:53
5

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!

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
0

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

AndyClaw
  • 740
  • 8
  • 15
  • I think this is my problem. Where did you fix it? In the database or in the EF code? And how exactly? – Francois Botha May 17 '14 at 22:00
  • I fixed it in the edmx. My database includes two tables, one being somewhat of an extension of the other, so the primary key value can be the same, but one of tables is more guaranteed to have a record for each primary key, whereas the second table is optional. I deleted the incorrect relationship (using the diagram worked) and added in the correct relationship. – AndyClaw May 19 '14 at 15:42
0

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.

Jeff Paulson
  • 1
  • 1
  • 1