【发布时间】:2016-09-27 12:40:04
【问题描述】:
是否可以在 ASP.NET Core 中使用新的依赖注入机制将 IdentityDbContext 注入到我的存储库中?
EF 版本:
"EntityFramework.Commands": "7.0.0-rc1-final",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
上下文:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext
{
public DbSet<Customer> Customers{ get; set; }
protected override void OnModelCreating(ModelBuilder builder) => base.OnModelCreating(builder);
}
存储库:
public class CustomerRepository: ICustomerRepository
{
private readonly IApplicationDbContext _context;
public CustomerRepository(IApplicationDbContext context)
{
_context = context;
}
public List<Customer> GetAll()
{
return _context.Customers.ToList();
}
}
如何配置 Startup.cs?
我有基本配置
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
这不起作用:
services.AddTransient<IApplicationDbContext, ApplicationDbContext>();
【问题讨论】:
标签: c# entity-framework dependency-injection asp.net-core asp.net-identity