2014-10-08 54 views
1

我想做一个泛型函数,这将有助于避免重复我的代码。该函数必须与通用类型T(也许是T2)通用,因为它必须引用正在创建的表中的不同字段。不过,我不知道这是否可能。通用函数添加实体到表 - 实体框架6和C#

比方说,我有以下实体添加到数据库:

List<Type_1> entities_1 = new List<Type_1>() 
{ 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    ... 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
}; 
entities_1.ForEach(e => dbContext.Types_1.Add(e)); 
dbContext.SaveChanges(); 
entities_1 = null; 

List<Type_2> entities_2 = new List<Type_2>() 
{ 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    ... 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
}; 
entities_2.ForEach(e => dbContext.Types_2.Add(e)); 
dbContext.SaveChanges(); 
entities_2 = null; 

etc. 

是否有可能使带有参数的功能:List<T1>dbContextT2,将负责创建不同的表的功能?也许这需要参数化这些Type_(n)Types_(n)。我想下面的东西,但编译器不支持T2接受它,并点:

private void addEntitiesToDbContext<T1,T2>(List<T1> ent, MyTypeContext dbContext) 
{ 
    ent.ForEach(e => dbContext.T2.Add(e)); 
    dbContext.SaveChanges(); 
} 

感谢



编辑:


我觉得我已经得到了采取空间道歉评论部分-.-

我刚刚意识到什么C Bauer说关于存储库模式。这是如果有人也对这个设计感兴趣,下面是我推荐的一个非常好的链接:Repository Pattern Explained。还有一个关于Repository Pattern设计here的问题。祝你好运。

PS。在Repository Pattern

回答

1

我希望这些会帮助你。

public interface IRepository<TEntity> 
    where TEntity : class 
{ 
} 

public abstract class RepositoryBase<TEntity> : IRepository<TEntity> 
    where TEntity : class 
{ 
    private DbContext dbContext; 

    private DbSet<TEntity> dbSet; 

    public IQueryable<TEntity> All 
    { 
     get 
     { 
      return dbSet; 
     } 
    } 

    public void InsertOrUpdate(TEntity entity) 
    { 
    } 
} 

public class Repository<T> : RepositoryBase<T> 
    where T : class 
{ 
    public Repository(DbContext context) 
    { 
     DbContext = context; 
     DbSet = context.Set<T>(); 
    } 
} 
+0

你好Tau。我不知道你为什么被打分,所以我给你打分。你的解决方案真的很先进。虽然我理解抽象类的概念,但仍然很难遵循。我想我需要更多时间,并会尽快破解它。感谢您的时间:) – Celdor 2014-10-08 11:31:42

+1

@Ziko - 存储库是一种设计模式,这是相当先进的,但应满足您的需求100%。在使用存储库时,输入'Repository entityTypeRepository = new Repository ();'其中entitytype是您试图对其执行CRUD操作的实体。虽然通常您的存储库实现会暴露SubmitChanges或SaveChanges。 – 2014-10-08 12:51:00

+0

嗨。我花了一些教程来了解这里的例子。现在没问题,但仍需要就这个例子提出两个问题。 *为什么RepositoryBase 继承自IRepository ?我的猜测是Interface提供的约束在这里是引用类型。另外,接口不允许实现字段。 *在Repository 中,我不明白输入:'DbContext = context; DbSet = context.Set ();' 不应该是:'DbContext dbContext = context; DbSet DbSet = context.Set ();'谢谢。 – Celdor 2014-10-25 16:47:30

0

另一个很好的来源,我想不出为什么这是行不通的:

1)建立一个共同的基类(或接口),称之为BaseEntity,这所有的实体从派生。

2)添加到您的上下文类:

IDbSet<BaseEntity> Entitys { get; set; } 

3)让你的Add方法采取的列表BaseEntity的

private void addEntitiesToDbContext(List<BaseEntity> ent, MyTypeContext dbContext) 
{ 
    ent.ForEach(e => dbContext.Entitys.Add(e)); 
    dbContext.SaveChanges(); 
} 
+0

好,它说:'“XXdbContext”不包含“T2”,没有扩展方法“T2”接受型“XXdbContext”的第一个参数的定义可以找到(是否缺少使用指令或汇编引用?)'。就像它说的dbContext没有T2的知识:/我会尝试你所建议的,但我在c#的初学者,所以它可能仍然是压倒性的:我希望它不会:p – Celdor 2014-10-08 10:42:54