我创建了一个.NET Core应用程序,并使用依赖注入和存储库模式将存储库注入到我的控制器。但是,我收到了一个错误:
InvalidOperationException:当试图激活'WebApplication1.Controllers.BlogController‘时,无法解析“WebApplication1.Data.BloggerRepository”类型的服务。
仓库:
public interface IBloggerRepository { ... }
public class BloggerRepository : IBloggerRepository { ... }控制器:
public class BlogController : Controller
{
private readonly IBloggerRepository _repository;
public BlogController(BloggerRepository repository)
{
_repository = repository;
}
public IActionResult Index() { ... }
}Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddScoped<IBloggerRepository, BloggerRepository>();
}我不知道我做错了什么。有什么想法吗?
发布于 2017-04-28 13:21:02
只有在与我一样的情况下,我才会使用现有的数据库进行EntityFramework教程,但是当在模型文件夹上创建新的数据库上下文时,我们需要在启动时更新上下文,如果您有用户身份验证的话,也需要在services.AddDbContext和AddIdentity中更新上下文。
services.AddDbContext<NewDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<NewDBContext>()
.AddDefaultTokenProviders();https://stackoverflow.com/questions/40900414
复制相似问题