locked
Why Does Entity Framework generate update clause to update foreign key RRS feed

  • Question

  • User-623162908 posted
    public class a
    {
      [Key]
      public int id{get;set;}
    }
    
    public class b
    {
      [Key]
      public int id{get;set;}
      
      public a  a_ID {get;set;}
    }
    
    public class c
    {
      [Key]
      public int id {get;set;}
      
      public b b_id{get;set}
    }

    the code snippet previously mentioned is the model.

    
    public void Insert(c c1)
    {
       db.c.Add(c1);
       db.Entity(c1.b_id).state = Entities.Unchanged;
       db.Entity(c1.b_id.a_id).state = Entities.Unchanged;
       db.SaveChanges();
    }

    This code snippet previously mentioned is the EF code.

    but I get error, because the ef try to update table b in database: update b set a_id = x where id =..../*ignore the follow code*/

    Question:

      Why Does Entity Framework generate update clause to update foreign key?

    Wednesday, November 16, 2016 8:47 AM

All replies

  • User-707554951 posted

    Hi CrazyOldPotato.

    From your description and code, you want to add c1 to  c table in database, if that the case. According the relationship between you class(a,b,c). I suggest you could following the steps:

    Using the following code to add a1 to database:

    using (Model2 context = new Model2())
                {
                    a a1 = new a { id = 1 };
                    context.a.Add(a1);
                    context.SaveChanges();
                }

    Then add b1 to database:

    using (Model2 context = new Model2())
                {
                    a a1 = context.a.Find(1);
                    b b1 = new b { id = 1, a_ID = a1 };
                    context.b.Add(b1);
                    context.SaveChanges();
                }

    Finally, you could add c1 to database without any error:

     using (Model2 context=new Model2())
                {           
                    b b1 = context.b.Find(1);
                    c c1 = new c { b_id = b1 };
                    context.c.Add(c1);
                    context.Entry(c1.b_id).State = EntityState.Unchanged;
                   // context.Entry(c1.b_id.a_ID).State = EntityState.Unchanged; This is not required for us.
                    context.SaveChanges();
                }

    For I couldn't restored your problem. If you still have problem about this, would you please provide detailed error message for us. So that we could solve your problem timely.

    Best regards

    Cathy

    Thursday, November 17, 2016 10:11 AM
  • User-623162908 posted

    no ,the EF will generate update clause and send this clause first

    Saturday, November 19, 2016 8:10 AM
  • User-707554951 posted

    Hi CrazyOldPotato,

    Are your try the code I provide, it work well in my application without generate any update clause.

    the EF will generate update clause and send this clause first

    would you please provide us with more information about your error message?

    Best regards

    Cathy

    Friday, December 2, 2016 9:05 AM