LINQ to Entity Insert with Foreign Keys
-
Hi there, Appologies in advance if this is a stupid question about how to insert with L2E. I have read lots, searched and have a book but simple, practical L2E tasks seem hidden in theory and a multitude of approaches. I've got the general idea and can do simple insertions with L2E. However when a second table with a relationship is involved I can't find the "best" way to perform an insertion, for the following situation... In the past I could sort this all out in a SPROC so the insertion was done in one DB trip. Example Scenario: Insert an Article armed with Article.Title, Article.Body and Account.Name. With L2E do I have to make a call to the DB to get the AccountID and then make a separate call to do the insertion? Table - Articles PK ArticleID FK AccountID Title << Body << Table - Accounts PK AccountID Name << Cheers, Ke
-
Hi there, Appologies in advance if this is a stupid question about how to insert with L2E. I have read lots, searched and have a book but simple, practical L2E tasks seem hidden in theory and a multitude of approaches. I've got the general idea and can do simple insertions with L2E. However when a second table with a relationship is involved I can't find the "best" way to perform an insertion, for the following situation... In the past I could sort this all out in a SPROC so the insertion was done in one DB trip. Example Scenario: Insert an Article armed with Article.Title, Article.Body and Account.Name. With L2E do I have to make a call to the DB to get the AccountID and then make a separate call to do the insertion? Table - Articles PK ArticleID FK AccountID Title << Body << Table - Accounts PK AccountID Name << Cheers, Ke
-
Hi there, Appologies in advance if this is a stupid question about how to insert with L2E. I have read lots, searched and have a book but simple, practical L2E tasks seem hidden in theory and a multitude of approaches. I've got the general idea and can do simple insertions with L2E. However when a second table with a relationship is involved I can't find the "best" way to perform an insertion, for the following situation... In the past I could sort this all out in a SPROC so the insertion was done in one DB trip. Example Scenario: Insert an Article armed with Article.Title, Article.Body and Account.Name. With L2E do I have to make a call to the DB to get the AccountID and then make a separate call to do the insertion? Table - Articles PK ArticleID FK AccountID Title << Body << Table - Accounts PK AccountID Name << Cheers, Ke
Hi designit99! maybe this example (based on your db structure) helps! :laugh:
Cls_Datos.Articles NewMasterReg = new Cls_Datos.Articles();//Data Context Articles Table
NewMasterReg.Title = "Some Title!";
NewMasterReg.Body = "Anything you want";
foreach (DataRow r in Dt_Denominaciones.Rows)//A Data table with the child data. in your case, Accounts of Articles
{
Cls_Datos.Accounts NewChildReg = new Cls_Datos.Accounts();
NewChildReg.Name = r["AccountName"].ToString();
NewMasterReg.Accounts.Add(NewChildReg);
}
Datos.Articles.InsertOnSubmit(NewMasterReg);
Datos.SubmitChanges();:-D good luck! :thumbsup:
-
Hi there, Thank you for the help. I'll read through your example. Regards
-
Hi designit99! maybe this example (based on your db structure) helps! :laugh:
Cls_Datos.Articles NewMasterReg = new Cls_Datos.Articles();//Data Context Articles Table
NewMasterReg.Title = "Some Title!";
NewMasterReg.Body = "Anything you want";
foreach (DataRow r in Dt_Denominaciones.Rows)//A Data table with the child data. in your case, Accounts of Articles
{
Cls_Datos.Accounts NewChildReg = new Cls_Datos.Accounts();
NewChildReg.Name = r["AccountName"].ToString();
NewMasterReg.Accounts.Add(NewChildReg);
}
Datos.Articles.InsertOnSubmit(NewMasterReg);
Datos.SubmitChanges();:-D good luck! :thumbsup:
Hi there, Thanks for replying! I'll give it a go. Regards