Hello,
I'm new to 3.5 framework. I'm developing one web based application in asp.3.5. There am using ADO.Net Entity framework for save and update. In my database there is lot table with relationship (Foreign key). My problem is how can we save foreign key field(parent table) value in child table. Because in child table object that key field not displaying. But i want save parent key field value in child table...Please help me.
Thanks,
Aani
Loading

Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 3, 2010, 8:16 PM EST
Well its not like that you are thinking the foreign key concept is based on check constraint. when you insert value in child it checks the value for its existence in parent table. This is what a foreign key constraint can do.
you are asking about inserting automatically from parent to child. this issue is not related to parent child relation ship in foreign key constraint.
you have to query on parent table based on some condition of child table. then use the parent PrimaryID to insert into child.
you have to do it by writing code for it. its not automatic process
hope you got my point.
Aani Aani11 reputationPosted Mar 3, 2010, 8:51 PM EST
Your explanation about foreign key relationship to child is correct. But my problem is not that. Here i want to save parent table key value to child tabel..Usually we need to create one object for child table and assign values into corresponding tables fields
childtbl child=new childtbl();
child.id=1;
child.name="Aani";
child.FKId=2;
but when i trying write above line (child.FKId=2). That FKId not showing in child object. This is what problem... How can i assign my parent table key into child table... hope you understand my problem...
--
Regards,
Anandh Viswanathan.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 3, 2010, 10:28 PM EST
send me your project i understand what you want but i think there is some other problem.
I can help you.
Aani Aani11 reputationPosted Mar 3, 2010, 11:48 PM EST
Ya just wait....mine is big project so i will sent small one but same scenario .....
Aani Aani11 reputationPosted Mar 4, 2010, 12:50 AM EST
Sorry for late reply...I have attached one small proj...Datebase backup in app_data folder...so u just create one like sample and restore that and change the web.config info....
Sam HobbsRank #107 · 20.5K reputationPosted Mar 4, 2010, 10:24 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Mar 4, 2010, 7:10 PM EST
First add a select after the insert to get the Id; something similar to the following. Note that "SCOPE_IDENTITY()" is a sp.
The result of executing that should produce a one-record result that has one field; the identity from the previous insert.
Aani Aani11 reputationPosted Mar 4, 2010, 7:39 PM EST
Thank you for replaying me....I know this SQL statements...But i want to do with ADO.Net entity framework.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 4, 2010, 9:10 PM EST
Tbl_Pateint pateint = new Tbl_Pateint();
pateint.PatName = TextBox1.Text;
sample.AddToTbl_Pateint(pateint);
sample.SaveChanges();
//after save this table i will get this primary key id.
int patid = pateint.PatID;
//This is child table info here pat id is Foreign key.
// Now i need to save patient id into this child table
Tbl_PatAddress Add = new Tbl_PatAddress();
Add.Address = TextBox2.Text;
Add.City = TextBox3.Text;
Add.State = TextBox4.Text;
Add.Zip = TextBox5.Text;
Add.PatAddID = patid;
sample.SaveChanges();
i have marked red the newly added/Changed code lines.
hope it helps you.
Aani Aani11 reputationPosted Mar 4, 2010, 9:37 PM EST
Thanks amith...But that is what my problem.
Add.PatAddID = patid;
Above line is problem line. That Add object does not contain definition for PatAddID (i.e) usually when press .(dot) after object it shows all the fields in our table but here that foreign key field only not showing. Not only for this table all the table in my db...Hope you understand my problem.
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 5, 2010, 2:50 AM EST
i think there is some problem over there i opened your code and when wrote some lines for code it show the foreign key id
for below line means for Add after pressing . PatAddId was there.
Add.PatAddId
i don't think any problem with it.
peter cho2 reputationPosted Jun 22, 2010, 2:39 PM EDT
Did you get this resolve?