【发布时间】:2015-01-14 15:58:53
【问题描述】:
我正在开发一个使用实体框架 6 的 asp.net vmc5 Web 应用程序。 现在我正试图让这些工作:-
定义通用存储库。
为每个 DBSet 类型创建一个专用的存储库,该存储库将派生自通用存储库。
为每个专用存储库创建一个接口。
使用 UnitOfwork 类,以便调用多个存储库类将导致生成单个事务。
我有一个DbSet 类型为SkillType。
于是我创建了如下界面:-
namespace SkillManagementp.DAL
{
public interface ISkillTypeRepository {
}
然后是以下通用存储库:-
namespace SkillManagement.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal SkillManagementEntities context;
internal DbSet<TEntity> dbSet;
public GenericRepository(SkillManagementEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}//code goes here...
以下 SkillTypeRepository:-
namespace SkillManagement.DAL
{
public class SkillTypeRepository : GenericRepository<SkillType> : ISkillTypeRepository
{
private SkillManagementEntities db = new SkillManagementEntities();
public void Dispose()
{
db.Dispose();
}
public void Save()
{
db.SaveChanges();
}
}
}
最后我创建了以下 UnitOfWork 类:-
namespace SkillManagement.DAL
{
public class UnitOfWork : IDisposable
{
private SkillManagementEntities db = new SkillManagementEntities();
private SkillTypeRepository skillTypeRepository;
public SkillTypeRepository SkillTypeRepository
{
get
{
if (this.skillTypeRepository == null)
{
this.skillTypeRepository = new SkillTypeRepository();
}
return skillTypeRepository;
}
}
public void Save()
{
db.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
db.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
但我收到以下错误:-
我无法为我的 SkillManagmentRepositry 类定义两个派生类。
此外,我在 SkillTypeRepository 中收到此错误:- SkillManagement.DAL.GenericRepository' 不包含采用 0 个参数的构造函数
【问题讨论】:
-
你的
SkillTypeRepository没有构造函数,但是GenericRepository有,也应该是public class SkillTypeRepository : GenericRepository<SkillType>, ISkillTypeRepository -
因为基类有一个构造函数,你需要在派生类中提供一个构造函数并调用
:base(params),见这里stackoverflow.com/questions/12051/… -
那么 SkillTypeRepository 构造函数的方法定义应该是什么?
标签: c# entity-framework entity-framework-6 unit-of-work