合并自我跟踪实体

时间:2012-05-17 09:10:03

标签: entity-framework merge self-tracking-entities

存储在数据库中的对象图和相同的对象图被序列化为二进制包。包通过网络传输到客户端,然后有必要合并包中的数据和数据库中的数据。

合并源代码:

        //objList - data from package
        var objectIds = objList.Select(row => row.ObjectId).ToArray();

        //result - data from Database
        var result = SomeService.Instance.LoadObjects(objectIds);

        foreach (var OSobj in objList)
        {
            var obj = result.Objects.ContainsKey(OSobj.ObjectId)
                ? result.Objects[OSobj.ObjectId]
                : result.Objects.CreateNew(OSobj.ObjectId);

            var targetObject = result.DataObjects.Where(x => x.ObjectId == OSobj.ObjectId).FirstOrDefault();

            targetObject.StopTracking();
            var importedProperties = ImportProperties(targetObject.Properties, OSobj.Properties);
            targetObject.Properties.Clear();
            foreach (var property in importedProperties)
            {
                targetObject.Properties.Add(property);
            }
            targetObject.StartTracking();
        }

        return result;

ImportProperties方法的代码:

static List<Properties> ImportProperties(
        IEnumerable<Properties> targetProperties,
        IEnumerable<Properties> sourceProperties)
    {
        Func<Guid, bool> hasElement = targetProperties
            .ToDictionary(e => e.PropertyId, e => e)
            .ContainsKey;


        var tempTargetProperties = new List<Properties>();
        foreach (var sourceProperty in sourceProperties)
        {
            if (!hasElement(sourceProperty.PropertyId))
            {
                sourceProperty.AcceptChanges();
                tempTargetProperties.Add(sourceProperty.MarkAsAdded());
            }
            else
            {
                sourceProperty.AcceptChanges();
                tempTargetProperties.Add(sourceProperty.MarkAsModified());
            }
        }

        return tempTargetProperties;
    }

服务器保存传入的更改,如下所示:

_context.ApplyChanges("OSEntities.Objects", entity);
_context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

当服务器尝试保存更改时发生异常:

  

AcceptChanges无法继续,因为对象的键值与ObjectStateManager中的另一个对象冲突。在调用AcceptChanges之前,请确保键值是唯一的。

但是如果我更改了ImportProperties方法的代码,则不会发生错误并且成功保存更改:

static List<Properties> ImportProperties(
        IEnumerable<Properties> targetProperties,
        IEnumerable<Properties> sourceProperties)
    {
        Func<Guid, bool> hasElement = targetProperties.ToDictionary(e => e.PropertyId, e => e).ContainsKey;


        var tempTargetProperties = new List<Properties>();
        foreach (var sourceProperty in sourceProperties)
        {
            if (!hasElement(sourceProperty.PropertyId))
            {
                var newProp = new Properties
                                  {
                                      ElementId = sourceProperty.ElementId,
                                      Name = sourceProperty.Name,
                                      ObjectId = sourceProperty.ObjectId,
                                      PropertyId = sourceProperty.PropertyId,
                                      Value = sourceProperty.Value
                                  };

                tempTargetProperties.Add(newProp);
            }
            else
            {
                var modifiedProp = new Properties
                                       {
                                           ElementId = sourceProperty.ElementId,
                                           Name = sourceProperty.Name,
                                           ObjectId = sourceProperty.ObjectId,
                                           PropertyId = sourceProperty.PropertyId,
                                           Value = sourceProperty.Value
                                       };

                modifiedProp.MarkAsModified();
                tempTargetProperties.Add(modifiedProp);
            }
        }

        return tempTargetProperties;
    }

为什么会有例外?

1 个答案:

答案 0 :(得分:0)

当您将对象图(具有n级深度导航属性的实体)传输到客户端应用程序时,实体将记录在各自的更改跟踪器中所做的任何更改。当实体(或对象图)被发送回应用程序的服务器端时,基本上你需要做的就是:

try
{
  using(Entities context = new Entities())
  {
    context.ApplyChanges(someEntity);
    context.SaveChanges();
  }
}
catch
{
  ...
}

我认为不需要上面发布的所有代码。您想用该代码实现什么目标?