Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ to Entity Insert with Foreign Keys

LINQ to Entity Insert with Foreign Keys

Scheduled Pinned Locked Moved LINQ
tutorialquestioncsharpdatabaselinq
5 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    designit99
    wrote on last edited by
    #1

    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

    N P 2 Replies Last reply
    0
    • D designit99

      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

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Although it's about performance, this[^] article shows a simple example of inserts with foreign keys involved. As long as the graphs are filled appropriately EF will take care of insert.


      I know the language. I've read a book. - _Madmatt

      D 1 Reply Last reply
      0
      • D designit99

        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

        P Offline
        P Offline
        PunkIsNotDead
        wrote on last edited by
        #3

        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:

        D 1 Reply Last reply
        0
        • N Not Active

          Although it's about performance, this[^] article shows a simple example of inserts with foreign keys involved. As long as the graphs are filled appropriately EF will take care of insert.


          I know the language. I've read a book. - _Madmatt

          D Offline
          D Offline
          designit99
          wrote on last edited by
          #4

          Hi there, Thank you for the help. I'll read through your example. Regards

          1 Reply Last reply
          0
          • P PunkIsNotDead

            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:

            D Offline
            D Offline
            designit99
            wrote on last edited by
            #5

            Hi there, Thanks for replying! I'll give it a go. Regards

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups