c# – 实体框架 – 保存对分离状态下相关对象的更改

前端之家收集整理的这篇文章主要介绍了c# – 实体框架 – 保存对分离状态下相关对象的更改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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);

对TerritoryDe​​scription的更改已成功保存,但对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();
        }
    }

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

猜你在找的C#相关文章