Ask a questionAsk a question
 

Answerinsert into multiple tables off a single entity?

  • Thursday, November 05, 2009 5:13 PMahager Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have found plenty of examples on the entity framework which provide details on a failry basic table structure.  I have a situation where after an insert into table A I need to take the PK from the inserted record of table A and insert it along with some other data into table B.  If I make a custom Entity which has a combination of fields from table A and table B and then add an association of the custom entity to the table A and table B entitys, will may data correctly insert and update?  Below is an example of my tables where table A is Crews and table B is VendorCrews.  I need to try and do this in a single action as both tables are managed from a single grid in the ui.  Let me know if this makes sense.  Thanks!!

    Create Table Crews
    (CrewId int PK,
    FirstName nvarhcar(100),
    LastName nvarchar(100),
    Bio nvarchar(255),
    Image binary)

    Create Table VendorCrews
    (VendorCrewId Int PK,
    VendorId int,
    CrewId int FK,
    IsActive bit)
    Adam

Answers

  • Monday, November 09, 2009 3:08 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Adam,

     

    My example is for the id field is not identity in DB.

    If your id filed is identity, you can insert like this,

     

                Articles article = new Articles() { ArticleName = "Entity" };

                Feedback feedback = new Feedback() { FeebackName = "ORM" };

                feedback.Articles = article;

     

                context.AddToFeedback(feedback);

                context.SaveChanges();

     

    If you want to insert in any scenario, please give the detail description and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Friday, November 06, 2009 6:03 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi ahager,

     

     

    Welcome to MSDN forums!

     

    Do you mean that you want to insert records into two related tables in a single action?

    If it is that case, you can take advantage of EF’s navigate property to achieve this.

     

    When you choose the two tables into the EDM, you’ll see that there is a 1:M association between the two tables.

     

    For example, we have two tables like this,

     

    [Articles]

    ArticleID     [PK]

    ArticleName

     

    [FeedBack]

    FeedbackID  [PK]

    FeedbackName

    ArticleID     [FK]

     

    We can insert feedback with article like the following ways,

     

     

    public static void Insert()

            {

                StudentEntities context = new StudentEntities();

     

                Articles article = new Articles() { ArticleID = 1, ArticleName = "Entity" };

                Feedback feedback = new Feedback() { FeebackID = 1, FeebackName = "ORM" };

     

                feedback.Articles = article;

     

                context.AddToFeedback(feedback);

                context.SaveChanges();

            }

     

            public static void Insert()

            {

                StudentEntities context = new StudentEntities();

     

                Feedback feedback = new Feedback() { FeebackID = 1, FeebackName = "ORM" };

                feedback.Articles = (from p in context.Articles where p.ArticleID == 1 select p).First();

     

                context.AddToFeedback(feedback);

                context.SaveChanges();

            }

     

    If your tables have FK constraint between their PKs, you can make the table tables into one entity according to this blog,

    http://blogs.msdn.com/simonince/archive/2009/03/23/mapping-two-tables-to-one-entity-in-the-entity-framework.aspx

     

     

    For more sample code, you can also download the samples of Entity Framework in CodeFex,

    http://cfx.codeplex.com/SourceControl/ListDownloadableCommits.aspx

     

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, November 06, 2009 8:20 PMahager Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This gets me closer but I still have some unresolved questions.  In you example what if ArticleID is no known until it is inserted into the article table.  In other words performing an insert and retreiving the @@Identity of the inserted record.  Then taking that value and inserting it into the feedback table.


    Adam
  • Monday, November 09, 2009 3:08 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Adam,

     

    My example is for the id field is not identity in DB.

    If your id filed is identity, you can insert like this,

     

                Articles article = new Articles() { ArticleName = "Entity" };

                Feedback feedback = new Feedback() { FeebackName = "ORM" };

                feedback.Articles = article;

     

                context.AddToFeedback(feedback);

                context.SaveChanges();

     

    If you want to insert in any scenario, please give the detail description and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 11, 2009 9:22 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Adam,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, November 13, 2009 4:41 PMahager Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I had to shift gears and work on another project for a while so I have not been able to get back to this yet.  I will let you know when I do.  Thanks!
    Adam