none
Setting Navigation Property = NULL with Lazy Loading Issue RRS feed

  • Question

  • Using Entity Framework 4.0 & MVC 3.

    Model.ParentID = ID of a valid Parent object in the DB.  This Parent object is associated with a valid Child Object.
    Model.ChildID = NULL, goal is to remove the relationship between Parent and Child.

    Current code: 

    Parent parent = context.Parents.Single(T=>T.ID == Model.ParentID); [Expect valid Parent]
    parent.Child = context.Children.SingleOrDefault(T=>T.ID == Model.ChildID); [Expect parent.Child to be set to NULL]
    context.SaveChanges(AcceptAllChangesAfterSave);

    Issue:

    The relationship between parent and child is not removed.  I am guessing that this is related somehow to lazy loading but I am unable to find any documentation or explanation as to why the relationship is not removed. 

     

     

    Thursday, December 22, 2011 4:31 PM

All replies

  • Hi AndrewPierro,

    Welcome!

    I'm not sure what relationships between your tables. If One to Many, you can call .Remove() method on Parent's navigation method:

    parent.Child.Remove(context.Children.FirstOrDefault(T=>T.ID == Model.ChildID));

    If one to one, I think you should delete the Child record from ChildSet:

    Context.Children.DeleteObject();

    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.

    Monday, December 26, 2011 2:42 AM
    Moderator
  • Hi,

    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.


    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, December 29, 2011 7:04 AM
    Moderator
  • Alan, sorry, I was out on break and just got back, thank you for checking in.   I should have made the relationship between the objects more clear and given a more specific example.

    I have a MVC controller which is responsible for maintaining a 'Spec'  It is responsible for setting the Spec 'Name', 'Spec Category' and 'Test Method'.

    This is the code block which I was using to try to perform the update on the 'Spec':

    spec.Name = Model.Name;
    spec.SpecCategory = context.SpecCategories.SingleOrDefault(T => T.ItemGuid == Model.SpecCategoryGuid);
    spec.TestMethod = context.TestMethods.SingleOrDefault(T => T.ItemGuid == Model.TestMethodGuid);

     

    context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

    The code block works great when Model.SpecCategoryGuid / Model.TestMethodGuid are non null/empty.

    However, when '.SingleOrDefault' returns 'Default' (Null) the change is not persisted to the database.

    Thursday, December 29, 2011 2:35 PM
  • Hi AndrewPierro,

    Thanks for your feedback.

    It's a strang logic in your code. SpecCategory and TestMethod are parent tables for Spec. We couldn't break the relationship by navigation property, as there are may many Specs map to one SpecCategory, so you couldn't update others relationships by the one.

    You can try as the follow way:

    spec.Name = Model.Name;
    var category=context.SpecCategories.SingleOrDefault(T => T.ItemGuid == Model.SpecCategoryGuid);
    if(category==null)
    {
    spec.SpecCategoryGuid = null;
    }
    else
    {
    spec.SpecCategory=category;
    }
    // the same logic for TestMethod
    var method= context.TestMethods.SingleOrDefault(T => T.ItemGuid == Model.TestMethodGuid); 
    if(method==null)
    {
    spec.TestMethodGuid=null;
    }
    else
    {
    spec.TestMethod =method;
    }
    spec.TestMethod = 
     context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
    

    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.

    Monday, January 2, 2012 3:44 AM
    Moderator
  • Then my real question is:

    Why is this invalid?
    spec.SpecCategory = null;

     


     

     

    Tuesday, January 3, 2012 2:40 PM
  • Hi AndrePierro,

    Thanks for your feedback.

    >>ec.SpecCategory =null;

    What's exception will be thrown in your hand. I demo a project to try to repro your scenario, my code just ignore this value without any action, I'm not sure your invalid.

    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.

    Wednesday, January 4, 2012 6:20 AM
    Moderator
  • I do not think we are on the same page.  Please let me know if I can clarify something better.

    Does Not Work (spec.SpecCategory is NOT persisted to be NULL):

    //Model.SpecCategoryGuid = null
    
    var specCategory = context.SpecCategories.SingleOrDefault(T => T.ItemGuid == Model.SpecCategoryGuid);
    spec.SpecCategory = specCategory;
    context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

     

    Does Work (spec.SpecCategory IS persisted to be NULL):

    //Model.SpecCategoryGuid = null
    
    var specCategory = context.SpecCategories.SingleOrDefault(T => T.ItemGuid == Model.SpecCategoryGuid);
    if (!Equals(spec.SpecCategory, specCategory))
    {
        spec.SpecCategory = specCategory;
    }
    context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

     


    The only difference between the two above statements is the 'Equals' check in the second statement.

    Question: Why does the first code block fail to persist the NULL / clear the spec.SpecCategory?

    [Thank you very much for working with me on this.]




    Wednesday, January 4, 2012 8:06 PM