0

我需要克隆主实体和子实体。我在 CodeProject 上遇到了这个似乎可以完成工作的解决方案,请参阅: http: //www.codeproject.com/Tips/474296/Clone-an-Entity-in-Entity-Framework-4

但是我使用的是 EF5 和 DBContext,而这段代码使用的是 EF4 和 EntityObject,所以我想知道我需要对其进行哪些更改?

代码是:

public static T CopyEntity<T>(MyContext ctx, T entity, bool copyKeys = false) where T : EntityObject
{
T clone = ctx.CreateObject<T>();
PropertyInfo[] pis = entity.GetType().GetProperties();

foreach (PropertyInfo pi in pis)
{
    EdmScalarPropertyAttribute[] attrs = (EdmScalarPropertyAttribute[])
                  pi.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false);

    foreach (EdmScalarPropertyAttribute attr in attrs)
    {
        if (!copyKeys && attr.EntityKeyProperty)
            continue;

        pi.SetValue(clone, pi.GetValue(entity, null), null);
    }
}

return clone;

}

调用代码是:

Customer newCustomer = CopyEntity(myObjectContext, myCustomer, false);

foreach(Order order in myCustomer.Orders)
{
Order newOrder = CopyEntity(myObjectContext, order, true);
newCustomer.Orders.Add(newOrder);
}

我在这里发帖是因为对这篇文章的反馈看起来不活跃,我相信这是一个任何 EF 专业人士都可以回答的问题。

提前谢谢了。

4

2 回答 2

2

如果您想使用 EF5 克隆实体DbContext,最简单的方法是:

//clone of the current entity values
Object currentValClone = context.Entry(entityToClone)
                                .CurrentValues.ToObject();

//clone of the original entity values
Object originalValueClone = context.Entry(entityToClone)
                                    .OriginalValues.ToObject();

//clone of the current entity database values (results in db hit
Object dbValueClone = context.Entry(entityToClone)
                                   .GetDatabaseValues().ToObject();
于 2013-07-24T10:39:04.230 回答
0

只有在您的实体属性中具有EdmScalarPropertyAttribute

或者,您可以使用MetadataWorkspace来获取实体属性

public static class EntityExtensions
{
    public static TEntity CopyEntity<TEntity>(DbContext context, TEntity entity, bool copyKeys = false)
        where TEntity : class
    {
        ObjectContext ctx = ((IObjectContextAdapter)context).ObjectContext;
        TEntity clone = null;
        if (ctx != null)
        {
            context.Configuration.AutoDetectChangesEnabled = false;
            try
            {
                clone = ctx.CreateObject<TEntity>();
                var objectEntityType = ctx.MetadataWorkspace.GetItems(DataSpace.OSpace).Where(x => x.BuiltInTypeKind == BuiltInTypeKind.EntityType).OfType<EntityType>().Where(x => x.Name == clone.GetType().Name).Single();

                var pis = entity.GetType().GetProperties().Where(t => t.CanWrite);
                foreach (PropertyInfo pi in pis)
                {
                    var key = objectEntityType.KeyProperties.Where(t => t.Name == pi.Name).FirstOrDefault();
                    if (key != null && !copyKeys)
                        continue;
                    pi.SetValue(clone, pi.GetValue(entity, null), null);
                }
            }
            finally
            {
                context.Configuration.AutoDetectChangesEnabled = true;
            }
        }
        return clone;
    }
}
于 2014-09-04T14:43:06.787 回答