没有配置数据库提供程序。身份和EF7

时间:2016-04-04 10:47:04

标签: c# asp.net entity-framework asp.net-identity

这与其他问题不同!

There was an error running the selected code generator: 'No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.'

在向项目添加另一个数据库时出现此错误。在此之前,我的项目在ASP.NET Identity 3中运行良好。

现在我有两个数据库,一个是自定义数据库:

public class ShopDbContext : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Producer> Producers { get; set; } 

    public ShopDbContext(DbContextOptions<ShopDbContext> options) : base (options)
    {
        Database.EnsureCreated();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Follow the default behaviour of SqlServer for Identity Columns (Auto Increment)
        modelBuilder.ForSqlServerUseIdentityColumns();
    }
}

和身份一:

public class UsersDbContext : IdentityDbContext<ApplicationUser>
{
    public UsersDbContext()
    {
        Database.EnsureCreated();
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>().ForSqlServerToTable("Users");
        builder.Entity<IdentityUserRole<string>>().ForSqlServerToTable("UserRoles");
        builder.Entity<IdentityUserLogin<string>>().ForSqlServerToTable("UserLogins");
        builder.Entity<IdentityUserClaim<string>>().ForSqlServerToTable("UserClaims");
        builder.Entity<IdentityRole>().ForSqlServerToTable("Roles");
    }
}

在我的Startup.cs中:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddEntityFramework().AddSqlServer()
        .AddDbContext<UsersDbContext>(options =>
            options.UseSqlServer(Configuration["DatabaseConnections:Identity"]))
        .AddDbContext<ShopDbContext>(options =>
            options.UseSqlServer(Configuration["DatabaseConnections:Shop"]));

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<UsersDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc();

    // IoC Container
    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
    services.AddTransient<UsersDbContext>();
    services.AddTransient<ShopDbContext>();
}

现在奇怪的是ShopDbContext工作正常但UsersDbContext给出了错误。

我对ShopDbContext的构造函数所做的事情也不可能与Identity 1一起使用。

修改

这是堆栈跟踪:

Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource)
Microsoft.Data.Entity.Internal.DbContextServices.<>c__DisplayClass6_0.<Initialize>b__0()
Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
Microsoft.Data.Entity.Internal.DbContextServices.get_DatabaseProviderServices()
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.GetProviderServices(IServiceProvider serviceProvider)
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_11(IServiceProvider p)
Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
Microsoft.Data.Entity.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
Microsoft.Data.Entity.Infrastructure.DatabaseFacade.EnsureCreated()
DigitalShop.Models.Db.UsersDbContext..ctor() in UsersDbContext.cs
namespace DigitalShop.Models.Db
{
    public class UsersDbContext : IdentityDbContext<ApplicationUser>
    {
        public UsersDbContext()
        {
            Database.EnsureCreated(); // <-- Error
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
--- End of stack trace from previous location where exception was thrown ---

编辑2:

如果我更改UsersDbContext以覆盖OnConfiguring方法,那么它实际上会创建数据库并且它可以正常工作(即使我没有删除Startup.cs中的行和应该这样做:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);
    optionsBuilder.UseSqlServer(
        "Server=(localdb)\\mssqllocaldb;Database=DigitalShop_Identity;Trusted_Connection=True;MultipleActiveResultSets=true");
}

现在问题是为什么设置Startup.cs的连接字符串不起作用?

编辑3:

在调试UsersDbContext期间,我注意到OnConfiguring方法在构造函数之前运行!我认为这就是为什么它永远不会执行Database.EnsureCreated();因此抛出异常。怎么可能?

0 个答案:

没有答案