如何将此EF克隆代码转换为使用DBContext而不是EntityObject?

时间:2013-07-24 09:48:51

标签: asp.net-mvc-3 entity-framework entity-framework-4 entity-framework-5

我需要克隆Master和Child Entities。我在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专业人士回答的问题。

非常感谢提前。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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;
    }
}