locked
Setting navigation property does not update underlying table RRS feed

  • Question

  • I am really unsure why this is happening, I have never had this problem before and I only have one object in my entire entity model (that i am aware of) experiencing the problem.  I am unsure what to do, please help?

     

    I have an entity that has a primary key consisting of 3 foreign keys.  The entity also has one other property (nothing unusal with it).  Unfortunately, when I call my model.SaveChanges, I receive foreign key constraint problems.  AFter investigating, I figured out what is happening, I just don't know why.  I will demonstrate with a quick example:

     

    I have objects types A, B, and C.  Each of these types have a primary key which is a GUID.  They have other properties as well (but nothing really special going on).


    I also have object type D.  Type D has navigation properties to objects of type A, B and C (three different nav properties).  It also has another property that is simply a float.

     

    I create a new object A, object B and object C.

    I now create a new object D.  I set object D.A = A, D.B = B and D.C = C.

    When I do this, the navigation property is properly set.  (Meaning, if I check what D.A, D.B and D.C are pointing to, they are all pointing to the correct objects).

    My problem is this... the entity framework creates private variables _A_ID, _B_ID, and _C_ID.  When I set the navigation property, the model does not update the internal variables accordingly.  In fact, they are not changed ever (unless I manually change it).  When I call SaveChanges, it reads the values from the variable as opposed to the nav property.

     

    I haven't a clue why this is the only object experiencing the problem.  It started to occur when I changed my ID's to guids (in preparation for a multi DB app).  How can I fix it?

     

    Thanks in advance for any help you can supply.  I am a little desperate as I am working on a tight deadline and this has been putting me back.

     

    Thanks,

    Nicholas

    Monday, March 10, 2008 11:30 PM

Answers

  • Hi Nicholas,

     

    I was unable to repro this issue using the following code:

     

    Code Snippet

    A a = new A { id = Guid.NewGuid() };

    B b = new B { id = Guid.NewGuid() };

    C c = new C { id = Guid.NewGuid() };

    D d = new D { A = a, B = b, C = c };

    context.AddToD(d);

    context.SaveChanges();

     

     

    I see the expected results:

     

    a.id = 211ed020-ee0c-4c2d-b044-9e37d7c8ebc0
    d.a_id = 211ed020-ee0c-4c2d-b044-9e37d7c8ebc0
    b.id = 1b36161e-ecb7-416a-9a64-9e36e8964446
    d.b_id = 1b36161e-ecb7-416a-9a64-9e36e8964446
    c.id = dd90febf-2682-412e-ae4d-97155e52e39f
    d.c_id = dd90febf-2682-412e-ae4d-97155e52e39f

     

    Are you attempting something similar? There might be an Entity Framework bug lurking here, so if possible I'd like to take a look at your model/mapping and code (feel free to forward the information to "cmeek at microsoft dot com").

     

    Thanks,

    -Colin

    Tuesday, March 11, 2008 3:39 PM
  •  

    Yes, what you are attempting is exactly my scenario.  I will email you the model shortly.
    Tuesday, March 11, 2008 3:46 PM

All replies

  • Hi Nicholas,

     

    I was unable to repro this issue using the following code:

     

    Code Snippet

    A a = new A { id = Guid.NewGuid() };

    B b = new B { id = Guid.NewGuid() };

    C c = new C { id = Guid.NewGuid() };

    D d = new D { A = a, B = b, C = c };

    context.AddToD(d);

    context.SaveChanges();

     

     

    I see the expected results:

     

    a.id = 211ed020-ee0c-4c2d-b044-9e37d7c8ebc0
    d.a_id = 211ed020-ee0c-4c2d-b044-9e37d7c8ebc0
    b.id = 1b36161e-ecb7-416a-9a64-9e36e8964446
    d.b_id = 1b36161e-ecb7-416a-9a64-9e36e8964446
    c.id = dd90febf-2682-412e-ae4d-97155e52e39f
    d.c_id = dd90febf-2682-412e-ae4d-97155e52e39f

     

    Are you attempting something similar? There might be an Entity Framework bug lurking here, so if possible I'd like to take a look at your model/mapping and code (feel free to forward the information to "cmeek at microsoft dot com").

     

    Thanks,

    -Colin

    Tuesday, March 11, 2008 3:39 PM
  •  

    Yes, what you are attempting is exactly my scenario.  I will email you the model shortly.
    Tuesday, March 11, 2008 3:46 PM