如何使一个资源库类以< T>在EF [英] How to make a repository for class with <T> in EF
问题描述
我有一个简单的类:
public class TestClass<T> {
public int ID { get; set; }
public T Value { get; set; }
}
时,可以使用EF代码优先和库模式,以节省这种元素SQL数据库
Is is possible to use EF Code First and Repository pattern to save this kind of elements to SQL database?
修改:
这似乎是DbSet的无法使用-notation创建。例如。
Edit: It seems like the DbSet's cannot be created using the -notation. E.g.
public class UnitOFWorkTestContext : DbContext {
...
public DbSet<TestClass<int>> TestClass_Int { get; set; }
...
}
给出错误:
Gives error:
的类型'UnitOFWorkTest.Models.TestClass`1 [System.Int32]'没有映射。检查类型尚未明确使用方法忽略或NotMappedAttribute数据注解排除。验证类型被定义为一类,是不是原始的,嵌套或通用的,不会从EntityObject继承。的
推荐答案
下面是我自己的工作代码副本。我不假装我的代码是完美的,但它是很方便和宽松有coupleness。此外,这将是代码相当大的数额,但不要害怕=)...
Below is the copy of my own working code. I do not pretending that my code is perfect, but it is handy and has loose coupleness. Also, it will be quite big amount of code, but do not be afraid =)...
IEntity:
public interface IEntity
{
int Id { get; set; }
}
AbstractRepository和接口:
public interface IRepository<T>
{
IEnumerable<T> List();
IEnumerable<T> List(Func<T, bool> pred);
T Get(int id);
T Get(Func<T, bool> pred);
void Add(T entity);
T Update(T entity);
void Delete(T entity);
void Delete(Func<T, bool> pred);
}
public abstract class AbstractRepository<TEntity, TContext> : IRepository<TEntity>
where TContext : DbContext, new()
where TEntity : class, IEntity
{
protected TContext context;
public AbstractRepository(UnitOfWork<TContext> unit)
{
context = unit.Context;
}
public virtual IEnumerable<TEntity> List()
{
return context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> List(Func<TEntity, bool> pred)
{
return context.Set<TEntity>().Where(pred);
}
public virtual TEntity Get(int id)
{
return context.Set<TEntity>().Find(id);
}
public virtual TEntity Get(Func<TEntity, bool> pred)
{
return context.Set<TEntity>().FirstOrDefault(pred);
}
public virtual void Add(TEntity entity)
{
if (entity.Id <= 0)
context.Entry(entity).State = System.Data.EntityState.Added;
}
public virtual TEntity Update(TEntity entity)
{
if (entity.Id > 0)
{
context.Entry(entity).State = System.Data.EntityState.Modified;
return entity;
}
return null;
}
public virtual void Delete(TEntity entity)
{
context.Entry(entity).State = System.Data.EntityState.Deleted;
}
public virtual void Delete(Func<TEntity, bool> pred)
{
foreach (var entity in List(pred))
{
Delete(entity);
}
}
}
混凝土仓库:
public class GroupRepository : AbstractRepository<Group, SurveyContext>
{
public GroupRepository(UnitOfWork<SurveyContext> unit)
: base(unit)
{
}
}
的DbContext的继任者:
public class SurveyContext: DbContext
{
public SurveyContext() : base("name=ApplicationConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Optional
modelBuilder.Configurations.Add(new Confuration1());
modelBuilder.Configurations.Add(new Confuration2());
modelBuilder.Configurations.Add(new Confuration3());
modelBuilder.Configurations.Add(new GroupConfiguration());
base.OnModelCreating(modelBuilder);
}
public DbSet<Group> Groups { get; set; }
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
工作单位:
public class UnitOfWork<TContext> : IDisposable
where TContext: DbContext, new()
{
public TContext Context { get; private set; }
public UnitOfWork(TContext context)
{
Context = context;
}
public UnitOfWork() : this(new TContext())
{
}
public void Commit()
{
Context.SaveChanges();
}
public void Dispose()
{
}
}
用法:
using (var unit = new UnitOfWork<SurveyContext>())
{
//ViewDataSynchronize.SynchronizeSectionsForGroup(context, model.SectionIds, model.Group);
var repo = new GroupRepository(unit);
repo.Add(model.Group);
unit.Commit();
}
这篇关于如何使一个资源库类以< T>在EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!