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
peter choPosted Jun 22, 2010, 6:39 PM
Did you get this resolve?
Amit ChoudharyPosted Mar 5, 2010, 7:50 AM
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.
Aani AaniPosted Mar 5, 2010, 2:37 AM
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 ChoudharyPosted Mar 5, 2010, 2:10 AM
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 AaniPosted Mar 5, 2010, 12:39 AM
Thank you for replaying me....I know this SQL statements...But i want to do with ADO.Net entity framework.
Sam HobbsPosted Mar 5, 2010, 12:10 AM
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.
Sam HobbsPosted Mar 4, 2010, 3:24 PM
Aani AaniPosted Mar 4, 2010, 5:50 AM
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....
Aani AaniPosted Mar 4, 2010, 4:48 AM
Ya just wait....mine is big project so i will sent small one but same scenario .....
Amit ChoudharyPosted Mar 4, 2010, 3:28 AM
send me your project i understand what you want but i think there is some other problem.
I can help you.
Aani AaniPosted Mar 4, 2010, 1:51 AM
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 ChoudharyPosted Mar 4, 2010, 1:16 AM
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.