The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact".
The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'.
The statement has been terminated.
Hi, all. This is my maiden post.
Some background:
I am doing a C# project using Entity Framework 4.0 in Visual Studio 2010 and SQL Server 2005 Expresss.
The project is from the book, Programming Entity Framework, Chapter 9, which uses an edmx model built on the BreakAway database.
The project uses <object> datasources to bind data, and the little code that is added is create and attach the related Contact record when a new Customer record is passed to the context. The context is updating edits fine, but an insert for a new <Customer> record throws this error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact". The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'.
The statement has been terminated.
<Contact> entity has a 1-0..1 relationship with <Customer> entity.
Contact.ContactID is both an EntityKey and Identity, while Customer.ContactID is an EntityKey and a Foreign Key.
EF is supposed to manage identity keys, primary and foreign keys, so if my edmx and database are both set up correctly, why am I getting this error?
And how can I check the contents of the context just before it calls SaveChanges?