none
Insert Master and detail entityframework RRS feed

  • Question

  • Hello

    I´m using VS 2012, entity framework and Mysql.

    I have tow tables: orders and details. Orders has autoincremet id

    My code is somthing like this:

    Orders order = new Orders();
    Detail detail = new Detail();
    Detail.Order = order;
    entidades.Orders.Add(order);
    entidades.Details.add(Detail);
    entidades.savechanges();

    whit this code I get an error, I try with :

    Orders order = new Orders();
    Detail detail = new Detail();
    order.Details.Add(detail);
    entidades.Orders.Add(order);
    entidades.savechanges();

    the same error.

    only work with this code:

    Orders order = new Orders();
    Detail detail = new Detail();
    entidades.Orders.Add(order);
    entidades.savechanges();
    Detail.Order = order;
    entidades.Details.add(Detail);
    entidades.savechanges();

    but I think that it´s not correct use savechanges

    thanks

    Wednesday, January 22, 2014 11:08 AM

Answers

  • Hello Inasa Felix,

    From your description, if I understand correctly, the Order and Detail should has a one to many navigation: Order:Detail=>1:N. So it should has a foreign key in your Detail table that may be named OrderID.

    With the one to many relationship, if we want to insert a record into detail table, we need to mark sure that it has the correspondent record in master table.

    So that is why it fails when you try to use the first and second Code fragments, there is not a correspondent record in master table.

    This is just to add a new entity to context rather database. After we call the SaveChange() method, it then will be inserted into database.

    entidades.Orders.Add(order);
    When you try to use the third Code fragment, it will first insert the corespondent record into the master table.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 23, 2014 3:27 AM
    Moderator

All replies

  • Hello Inasa Felix,

    From your description, if I understand correctly, the Order and Detail should has a one to many navigation: Order:Detail=>1:N. So it should has a foreign key in your Detail table that may be named OrderID.

    With the one to many relationship, if we want to insert a record into detail table, we need to mark sure that it has the correspondent record in master table.

    So that is why it fails when you try to use the first and second Code fragments, there is not a correspondent record in master table.

    This is just to add a new entity to context rather database. After we call the SaveChange() method, it then will be inserted into database.

    entidades.Orders.Add(order);
    When you try to use the third Code fragment, it will first insert the corespondent record into the master table.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 23, 2014 3:27 AM
    Moderator
  • Thanks

    Then, the third code fragment it´s correct?

    Thursday, January 23, 2014 8:04 AM
  • Hello,

    >>Then, the third code fragment it´s correct?

    Yes, it is correct, because it will firstly insert the matser record into database.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 23, 2014 10:30 AM
    Moderator
  • Did you know that datasets are not a part of the EntityFramework.

    For that is also a special forum.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


    Success
    Cor

    Friday, January 24, 2014 11:02 AM