Click here to Skip to main content
15,071,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
FK relationship error in ADO.NET Entities when Insert new Record.

I m getting this error:
Entities in 'EntitiesSet' participate in the 'FK_Detail_Owner' relationship.  0 related 'Owner' were found. 1 'Owner' is expected


DataContext db = new DataContext(); 
Detail DV = new Detail(); 
try 
{ 
DV.DetailCode = txtdetCode.Text; 
DV.Name = txtname.Text; 
DV.Address = txtAdd.Text; 
DV.Phone = txtPhone.Text; 
db.AddToDetail(DV); 
db.SaveChanges(); 
} 
Catch(SqlException ex) 
{ 
throw ex; 
}


DetailCode is FK and PK in Detail Table, And OwnerID is PK in Owner table.
Posted
Updated 12-Aug-11 5:48am
v5

Looks like you are trying to insert something into a foreign key which is not available in the primary key field of another table.
   
Comments
divesh12 12-Aug-11 10:05am
   
this is my Code-

DataContext db = new DataContext();
Detail DV = new Detail();
try
{
DV.DetailCode = txtdetCode.Text;
DV.Name = txtname.Text;
DV.Address = txtAdd.Text;
DV.Phone = txtPhone.Text;

db.AddToDetail(DV);
db.SaveChanges();
}
Catch(SqlException ex)
{
throw ex;
}


DetailCode is FK with other table Owner.
[no name] 12-Aug-11 10:24am
   
You should edit your question and put this there.
divesh12 12-Aug-11 10:51am
   
Put now...
Аslam Iqbal 12-Aug-11 10:58am
   
As OP asked this is the best answer.+5
divesh12 12-Aug-11 11:00am
   
Which one and who is OP?
thatraja 12-Aug-11 11:56am
   
/*Which one?*/
Abhinav 's answer.
/*and who is OP?*/
You
divesh12 12-Aug-11 12:17pm
   
Theraja i also dont know about Aslam Comment.
thatraja 12-Aug-11 12:39pm
   
My name is thatraja.
OK. Actually Abhinav gave you right answer. You can't store a value in child table which key not exists in primary table, because you have created a constraint.

OP mean Original poster(or Enquirer)....you.

Anyway see my answer.
divesh12 12-Aug-11 12:42pm
   
I insert data in Detail table in this table DetailCode is FK and PK also. One more table Owner in this table OwnerID is PK. When i Insert data in Detail table i got this error.
thatraja 12-Aug-11 12:49pm
   
Check my answer
divesh12 12-Aug-11 12:50pm
   
As your answer, Value i enter in FK its avalible in other table PK.
Аslam Iqbal 13-Aug-11 0:46am
   
Thank you.
It seems very bloody obvious. No Owner has been associated with the object you are attempting to insert.
   
Comments
divesh12 12-Aug-11 10:10am
   
mark how to solve it.
[no name] 12-Aug-11 10:27am
   
Obviously the Detail entity requires an Owner be associated with it. So assign one.

It's your code, you should know how to get the Owner and assign it to the Detail object.
Take a look at PRIMARY KEY Constraint[^] & FOREIGN KEY Constraint[^] in SQL.

Tutorial says:
The FOREIGN KEY constraint also prevents that invalid data form being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.


Here you are trying to insert some value into the column(foreign key) which is not present in the column(primary key) of another table, but it raised you an exception

Parent Table
---------------
ColumnA ColumnB
100     1000
200     2000

Child Table
---------------
ColumnC ColumnD ColumnA
1       1       100  <---- Value 100 exists in Parent table
2       2       300  <---- But value 300 not exists in Parent table, so it'll raise error. So insert existing value here
   
Comments
divesh12 12-Aug-11 12:53pm
   
As your answer, Value i enter in FK its avalible in other table PK.
thatraja 12-Aug-11 12:58pm
   
Then probably you are sending wrong values in your code. Put breakpoint in your code, debug & check the values.
divesh12 12-Aug-11 13:00pm
   
Every thing is fine i already check that. I am so much confused now how to solve.
divesh12 12-Aug-11 12:57pm
   
ThatRaja accully i have two FK in table. Both are connected to diffrent2 table. Other table one one PK.
thatraja 12-Aug-11 13:05pm
   
You mean two FK fields referring two PK in different table?
Then
FK 1 value must exists in PK 1 column &
FK 2 value must exists in PK 2 column.
Check it
divesh12 12-Aug-11 13:09pm
   
I have Three Table 1. Owner 2. Detail 3. Resource
Owner and Resource table one one PK Column. I Insert Data in Detal Table. Detail table have two FK column field.
thatraja 12-Aug-11 13:13pm
   
OK.
Now
your Detail Table's FK 1 column value must exists in Owner Table &
your Detail Table's FK 2 column value must exists in Resource Table
check it.
divesh12 12-Aug-11 13:14pm
   
Yes
thatraja 12-Aug-11 13:18pm
   
Check in your coding. I'm sure problem in your code. Something like you are sending blank string or null values mistakenly to those FK columns. Please debug step by step till the end. That's all.
divesh12 12-Aug-11 13:20pm
   
If any of FK field blank. Its show message. i added mesagebox with If condition for that.
Аslam Iqbal 13-Aug-11 0:50am
   
This is a very primitive issue. Better tell OP to buy & read a book of RDBMS.
thatraja 14-Aug-11 13:38pm
   
You right but he came with code & error message so I tried to fix

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900