简体   繁体   English

将实体添加到表的通用函数-实体框架6和C#

[英]Generic function adding entities to tables - Entity Framework 6 and C#

I am trying to make a generic function which would help to avoid a repetition in my code. 我正在尝试创建一个通用函数,这将有助于避免代码重复。 The function has to be generic with general type T (and perhaps T2) as has to refer to different fields in tables being created. 该函数必须具有通用类型T(可能还有T2),并且必须引用要创建的表中的不同字段。 I don't know if it's possible, though. 我不知道是否有可能。

Let's say I have following entities to add to a database: 假设我有以下实体要添加到数据库中:

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.

Is it possible to make a function with parameters: List<T1> and dbContext referring to T2 , a function that would handle creating different tables? 是否可以使用以下参数制作一个函数: List<T1>dbContext引用T2 ,该函数可以处理创建不同的表? Perhaps that would require to parametrize those Type_(n) and Types_(n) . 也许这需要参数化Type_(n)Types_(n) I tried something below but compiler does not accept it and points at T2: 我在下面尝试了一些方法,但是编译器不接受它,并指向T2:

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

Thanks 谢谢



EDIT: 编辑:


I feel like I've got to apologise for taking space in the comment section -.- 我觉得我必须为在评论部分中的空格表示歉意-.-

I have just realised what C Bauer said regarding the Repository Pattern . 我刚刚意识到C Bauer关于存储库模式的看法。 This is If someone is also interested in this design, here's a very good link I recommend: Repository Pattern Explained . 如果有人也对此设计感兴趣,这是我推荐的一个很好的链接: Repository Pattern Explained There's also a question regarding the Repository Pattern design here . 这里还有一个关于存储库模式设计的问题 Good luck. 祝好运。

PS. PS。 Another very good source on Repository Pattern 仓库模式的另一个很好的来源

I hope those will help you. 希望这些对您有所帮助。

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>();
    }
}

I can't think why this wouldn't work: 我不知道为什么这行不通:

1)Create a common base class (or interface), call it BaseEntity, which all your entities derive from. 1)创建一个通用的基类(或接口),将其称为BaseEntity,所有实体都派生自该基类。

2)Add this to your context class: 2)将此添加到您的上下文类中:

IDbSet<BaseEntity> Entitys { get; set; }

3)Have your Add method take a list of BaseEntity's 3)让您的Add方法获取BaseEntity的列表

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM