locked
EF 4.1 Insert into multiple related entities RRS feed

  • Question

  • I am using the DBContext and I want to insert into to multiple entities.

    I assumed that related entities would be recognized and use for the insert but it is not working for me.

    The entities that I am having problems with are as follows:

    Case

    CaseID PK

    CaseSuspectID FK

     

    CaseSuspect

    CaseSuspectID PK

    SuspectID FK

     

    Suspect

    SuspectID PK

     

      internal void InsertSuspect(Suspect s, Case c)
            {
                Entities db = new Entities();
    
         
                db.Suspects.Add(s);
                db.Cases.Add(c);
    
                
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
            }
    

    The suspect gets added and the case gets added but there is no record in the CaseSuspect entity like I expected. Do I need to explicitly add the CaseSuspect table with the proper values? I don't know what the values are until the suspect gets inserted.

    Tuesday, November 1, 2011 8:57 PM

Answers

  • Hi forwheeler,

    Welcome!

    According to your description, the relationship among the three tables is: Suspect:CaseSuspect=1:M, CaseSuspect:Case=1:M;

    If you are use Code-First in your application, you should use each navigation to insert the related entities.

    internal void InsertSuspect(Suspect s, Case c)
            {
                Entities db = new Entities();
                Suspect temps= s;
                CaseSuspect cs= new CaseSuspect(){CaseSuspectID=c.CaseSuspectID, Cases=new List<Case>()}; 
                cs.Cases.Add(c);
                temps.CaseSuspects.Add(cs);
                 db.Suspects.Add(temps);
                //db.Cases.Add(c);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
            }
    
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by forwheeler Thursday, November 3, 2011 12:39 PM
    Wednesday, November 2, 2011 8:42 AM

All replies

  • Hi forwheeler,

    Welcome!

    According to your description, the relationship among the three tables is: Suspect:CaseSuspect=1:M, CaseSuspect:Case=1:M;

    If you are use Code-First in your application, you should use each navigation to insert the related entities.

    internal void InsertSuspect(Suspect s, Case c)
            {
                Entities db = new Entities();
                Suspect temps= s;
                CaseSuspect cs= new CaseSuspect(){CaseSuspectID=c.CaseSuspectID, Cases=new List<Case>()}; 
                cs.Cases.Add(c);
                temps.CaseSuspects.Add(cs);
                 db.Suspects.Add(temps);
                //db.Cases.Add(c);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
            }
    
    

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by forwheeler Thursday, November 3, 2011 12:39 PM
    Wednesday, November 2, 2011 8:42 AM
  • Thanks Alan, that works. I am using the Database first model so I changed

     

     CaseSuspect cs= new CaseSuspect(){CaseSuspectID=c.CaseSuspectID, Cases=new List<Case>()}; 
    

    to

     CaseSuspect cs= new CaseSuspect(); 
    

    I also didn't create the temps suspect but just used the suspect parameter I already have.

     

             internal void InsertSuspect(Suspect s, Case c)
            {
                Entities db = new Entities();
    
                CaseSuspect cs = new CaseSuspect();
    
                cs.Cases.Add(c);
                s.CaseSuspects.Add(cs);
    
                db.Suspects.Add(s);
                     
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
            }
        

    It seems I need to explicitly setup the related tables in the code like you listed since EF can't seem to determine the relationship otherwise. If I have a many to many link table would I need to do the same?

    Learning by trial and error takes a long time and the online samples seem to show very simple scenarios. Do you have any recommendations?

    Wednesday, November 2, 2011 2:23 PM
  • Hi forwheeler,

    Thanks for your feedback.

    You can refer this link for Many to Many relationship: http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx

    For insert, you also need use Navigation to add related collection.

    http://forums.asp.net/t/1664702.aspx/1?Insert+Many+To+Many+Entity+Framework

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 3, 2011 7:33 AM
  • I was hoping that there would be updated documentation on many to many using DBContext in EF4.1. Maybe nothing has changed so it is not necessary.

     

    Thanks for the links and the help.

    Thursday, November 3, 2011 12:38 PM