EF4 POCO Not Updating Navigation Property On Save

Posted by Gavin Draper on Stack Overflow See other posts from Stack Overflow or by Gavin Draper
Published on 2011-01-05T09:48:10Z Indexed on 2011/01/05 9:54 UTC
Read the original article Hit count: 232

Filed under:
|

I'm using EF4 with POCO objects the 2 tables are as follows

Service ServiceID, Name, StatusID

Status StatusID, Name

The POCO objects look like this

Service ServiceID, Status, Name

Status StatusID, Name

With Status on the Service object being a Navigation Property and of type Status.

In my Service Repository I have a save method that takes a service objects attaches it to the context and calls save. This works fine for the service, but if the status for that service has been changed it does not get updated. My Save method looks like this

public static void SaveService(Service service)
{
    using (var ctx = Context.CreateContext())
    {
        ctx.AttachModify("Services", service);
        ctx.AttachTo("Statuses",service.Status);
        ctx.SaveChanges();
    }
}

The AttachModify method attaches an object to the context and sets it to modified it looks like this

public void AttachModify(string entitySetName, object entity)
{
    if (entity != null)
    {
        AttachTo(entitySetName, entity);
        SetModified(entity);
    }
}

public void SetModified(object entity)
{
    ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}

If I look at a SQL profile its not even including the navigation property in the update for the service table, it never touches the StatusID. Its driving me crazy. Any idea what I need to do to force the Navigation Property to update?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about POCO