实体框架 - 保存对分离状态中相关对象的更改

c# entity-framework northwind

我正在使用Northwind数据库作为这篇文章的一个例子,我在使用实体框架将分离的实体保存回数据库时遇到了问题。

我有以下两种方法来获取地区和地区:

    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();
        }
    }

这些方法都工作正常,让我在分离状态下获得我需要的对象集合。

我还有一个名为SaveEntity的静态方法,它接受旧实体和当前编辑的实体,如下所示:

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

此方法部分适用于将对象的更改保存到数据库的位置,但不保存对相关对象关系的任何更改。

我有以下代码调用上面的方法作为我的例子:

            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);

对TerritoryDescription的更改已成功保存,但对Region的更改不是,在数据库中它仍然保留为RegionID = 1而不是RegionID = 2。

任何人都可以向我提供一些见解,说明为什么ApplyPropertyChanges不会传播对相关对象的更改?

另外,有谁知道如何解决这个问题?

热门答案

不是单独获取区域和区域,而是在同一查询中获取它们。类似的东西(我假设您想要更新实体,不想创建新的实体);

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

然后将它们更新为以下内容;

   List<Territories> territoriesWithRegions = GetTerritoriesWithRegions();
        Territories territory = territories.Where(n => n.TerritoryID == "01581").FirstOrDefault();
        territory.TerritoryDescription = "Hello World";
        Region region = territories.Where(p => p.Any(q => q.Region.RegionID == 2)).FirstOrDefault().Region;
        territory.Region = region;

        SaveEntity(territory);

并拯救他们;

static void SaveEntity(EntityObject entity)
    {
        using (NorthwindEntities entities = new NorthwindEntities())
        {
            entities.Attach(entity);
            entities.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);            
            entities.SaveChanges();
        }
    }

我在记事本上编码了这个,所以可能有错误;如果有的话请评论,所以我会相应更新。



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因