473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_..."

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?
Nov 28 '10 #1

✓ answered by Bryan Cheung

Hi Good guy,

The error is telling you that you first have to add a contact row so you have a ContactID and afterwards you can insert a Customer with the ContactID. Unless you are using a existing ContactID. This has to do with relationships between tables and I believe your Customer.ContactID is set to "not null" so it requires a (existing)ContactID.

You just have to or set CustomerID.ContactID to nullable(if not every customer has a contact) or use a existing contactid, so insert contact row before customer row or switch relation to Contact -> Customer which means a Contact.CustomerID field and remove the Customer.ContactID field. Depends on your requirements which choice you make.

Good luck,
B.

2 15293
Hi Good guy,

The error is telling you that you first have to add a contact row so you have a ContactID and afterwards you can insert a Customer with the ContactID. Unless you are using a existing ContactID. This has to do with relationships between tables and I believe your Customer.ContactID is set to "not null" so it requires a (existing)ContactID.

You just have to or set CustomerID.ContactID to nullable(if not every customer has a contact) or use a existing contactid, so insert contact row before customer row or switch relation to Contact -> Customer which means a Contact.CustomerID field and remove the Customer.ContactID field. Depends on your requirements which choice you make.

Good luck,
B.
Nov 29 '10 #2
Thanks, Bryan, for the info. I went over the code and found the cause of my bug - a small typo in my code!
Nov 29 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
3
by: Phil Powell | last post by:
$sql = 'INSERT INTO fs_usermetadata (' . substr(trim($cols), 0, strrpos(trim($cols), ',')) . ') VALUES (' . substr(trim($values), 0, strrpos(trim($values), ',')) . ')'; if (!mysql_query($sql))...
6
by: Dave | last post by:
I have a stored procedure in sql 2000 that requires steps to be fully completed before moving to the next command in the procedure. I have tried to place the word "GO" after each statement. When I...
8
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
6
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert...
1
by: DJ DIGI | last post by:
Hello Everyone, I'm attempting to design a small web app to obtain customer feedback. The issue is when a user inputs >4000 characters in the COMMENTS section I get the following Err Msg: ...
2
by: matt | last post by:
Any help appreciated. I have a form that does pretty well to collect data and upload a persons picture to a directory on a server. The part of the form that does the latter is: ...
3
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
3
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID ...
1
by: bougie | last post by:
i have two tables projects and proposals in project I have column ID and in proposal I have column ID too and there is a one to one realtionship between these tables the primary key in this realtion...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.