views:

787

answers:

1

I'm using the Northwind database as an example for this post where I am having a problem saving detached entities back to the database using the entity framework.

I have the following two methods to get me the territories and the regions:

    static List<Region> GetRegions()
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            entities.Region.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            return entities.Region.ToList();
        }
    }

    static List<Territories> GetTerritories()
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            entities.Territories.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            return entities.Territories.ToList();
        }
    }

These methods both work fine and get me the collection of objects I require in the detached state.

I also have a static method called SaveEntity, which takes in both an old entity and the currently editted entity, this is as follows:

    static void SaveEntity(EntityObject oldEntity, EntityObject newEntity)
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            entities.Attach(oldEntity);
            entities.ApplyPropertyChanges(newEntity.EntityKey.EntitySetName, newEntity);
            entities.SaveChanges();
        }
    }

This method partially works where the changes to the object are saved down to the database, but any changes to the relationship of related objects are not saved.

I have the following code calling the above methods as my example:

            List<Territories> territories = GetTerritories();
        List<Region> regions = GetRegions();

        Region region = regions.Where(n => n.RegionID == 2).FirstOrDefault();
        Territories oldTerritory = territories.Where(n => n.TerritoryID == "01581").FirstOrDefault();
        Territories newTerritory = ObjectCopier.Clone<Territories>(oldTerritory);

        newTerritory.TerritoryDescription = "Hello World";
        newTerritory.Region = region;

        SaveEntity(oldTerritory, newTerritory);

The change to TerritoryDescription is successfully saved, but the change to Region is not, in the database it still remains as RegionID=1 instead of RegionID=2.

Can anyone provide me with some insight to why ApplyPropertyChanges doesn't propogate changes to related objects?

Also, does anyone know of how I can get around this problem?

A: 

I think you can find answers here (Alex James is better one). http://stackoverflow.com/questions/1612655/entity-framework-updating-with-related-entity

Basically, the reason is because in EF relationships are objects too and have statuses like entities (deleted, added,...), so you would need to have the original reference value in the context as well.

Misha N.