【问题标题】:Entity Framework 4, Self Tracking Entities T4 Template, ApplyChanges() extension methodEntity Framework 4,自我跟踪实体 T4 模板,ApplyChanges() 扩展方法
【发布时间】:2013-09-29 23:54:33
【问题描述】:

我正在使用 EF4 和内置的自我跟踪实体模板从我的模型中生成我的实体。我还修改了 T4 模板,以便所有对“ObjectContext”的引用都更改为“IObjectContext”,并为自动生成的上下文应用了一个接口(所有这些都是为了测试和模拟目的)。

//my interface
public interface IDatabaseEntities
{
    IObjectSet<Customer> Customers {get;}
    int SaveChanges();
}

//self tracking entity auto gen code, with my mods
public partial class DatabaseEntities : ObjectContenxt, IDatabaseEntities
{
    //auto gen stuff here
    public IObjectSet<Customer> Customers
    //more auto gen stuff
}

在 T4 模板中,他们生成了一个扩展方法 ApplyChanges(),该方法仅适用于“ObjectSet”类型的对象。所以我不能调用“_context.Customers.ApplyChanges(customer);”因为我正在使用“IObjectSet”类型。我真的需要调用这个方法来更新一个分离的实体!!!所以现在我不知道如何更新我的实体,因为我没有使用具体的 ObjectSet 类。

【问题讨论】:

    标签: entity-framework t4


    【解决方案1】:

    像下面这样的扩展方法怎么样:

    public static class EntityFrameworkExtensions
    {
        public static void ApplyChanges<TEntity>(this IObjectSet<TEntity> objectSet, TEntity entity)
            where TEntity : class
        {
            if (objectSet is ObjectSet<TEntity>)
            {
                ((ObjectSet<TEntity>)objectSet).ApplyChanges(entity);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多