Click here to Skip to main content
15,886,032 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.
 
Share this answer
 
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?
It seems very bloody obvious. No Owner has been associated with the object you are attempting to insert.
 
Share this answer
 
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
 
Share this answer
 
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

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