不幸的是,我发现自己遇到了一个问题fella,但略有不同。 为什么我说不同,因为我正在尝试与我的应用程序相同的事情,除了我没有使用UnitOfWork。 所以我有一个BaseContext.cs:
public class BaseContext<TContext>: IdentityDbContext<User> where TContext: IdentityDbContext<User>, IRealContext{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext()
: base("myDB")
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = true;
Database.SetInitializer(new MigrateDatabaseToLatestVersion<RealContext, RealContextMigrationConfiguration>());
}
}
BaseRepository,如:
public class BaseRepository<T, C> : IEntityRepository<T>
where T: class
where C: BaseContext<C>
{
protected BaseContext<C> _ctx;
protected DbSet<T> EntitySet { get; set; }
public BaseRepository(IRealContext_ctx)
{
if (_ctx == null)
throw new ArgumentNullException("DbContext is null.");
this._ctx = _ctx as BaseContext<C>;
EntitySet = this._ctx.Set<T>();
}
DI IoC with Ninject:
kernel.Bind(typeof(IEntityRepository<>)).To(typeof(BaseRepository<,>)).InRequestScope();
kernel.Bind(typeof(IRealContext)).To(typeof(BaseContext<>)).InRequestScope();
ProductsController:
[RoutePrefix("api/products")]
public class ProductsController : BaseApiController<Product>
{
public ProductsController()
{
}
public ProductsController(IEntityRepository<Product> repo)
: base(repo)
{
}
我有一个测试:
private IEntityRepository<Product> repo;
ProductsController controller;
Guid g = Guid.NewGuid();
[TestInitialize]
public void SetUp()
{
var ctx = new FakeContext();
repo = new BaseRepository<Product, FakeContext>(ctx);
controller = GenericController.GetGenericController<ProductsController, Product>(repo);
populateFakeSet();
}
FakeContext:
public class FakeContext: BaseContext<FakeContext>, IRealContext
{
public FakeContext()
{
Products = new FakeDbSet<Product>();
}
public DbSet<Product> Products { get; set; }
我的测试通过,不幸的是,当我尝试运行我的应用程序,所有内容编译,启动时,产品控制器上的请求返回:
消息:&#34;发生错误。&#34;,exceptionMessage:&#34;发生错误 在尝试创建类型为&#39; ProductsController&#39;的控制器时。 确保控制器具有无参数的公共构造函数。&#34; exceptionType:&#34; System.InvalidOperationException&#34;,innerException:{ 消息:&#34;发生错误。&#34;,exceptionMessage:&#34;数量 提供的泛型参数不等于泛型类型的arity 定义。参数名称:instantiation&#34;,
BUMP。
如果有人提示,请帮助兄弟。 :( 这是我的第10个小时,因为我正在处理这个愚蠢的事情...... :( 或者另一种方法? 我使用BaseRepository,以便我可以发送/注入FakeContextt用于测试,并将BaseContext作为真正的DB容器。 我错过了什么吗? (我是EF新手,在stackoverflow上,我现在搜索了一段时间,我发现了一些解释,我不完全理解这些例子/问题或一些答案。)