C# 未配置任何数据库提供程序。身份与EF7
这与其他问题不同强>C# 未配置任何数据库提供程序。身份与EF7,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,这与其他问题不同 运行所选代码生成器时出错: '未配置任何数据库提供程序。在设置服务时,通过覆盖DbContext类或AddDbContext方法中的OnConfigurang来配置数据库提供程序。“ 将另一个数据库添加到我的项目时出现此错误。在此之前,我的项目与ASP.NET Identity 3配合得很好 现在我有两个数据库,一个自定义数据库: public class ShopDbContext : DbContext { public DbSet<Category>
运行所选代码生成器时出错:
'未配置任何数据库提供程序。在设置服务时,通过覆盖DbContext类或AddDbContext方法中的OnConfigurang来配置数据库提供程序。“
将另一个数据库添加到我的项目时出现此错误。在此之前,我的项目与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();
}
}
编辑2:
如果我将UsersDbContext
更改为同时覆盖onconfigurang
方法,它实际上会从那里创建数据库并工作(即使我没有从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
的过程中,我注意到onconfigurang
方法在构造函数之前运行!我想这就是为什么它从不执行Database.EnsureCreated()
并因此引发异常。这怎么可能呢???请添加错误并显示IdentityDbContext。@DenisBubnov显示它?对不起IdentityDbContext。我指的是一条消息。堆栈跟踪-很好,但什么是错误消息?@DenisBubnov,这个问题的标题是错误消息!不是我。我跟踪了问题。我没有不要使用EF7,我对这个问题感兴趣。我投你一票
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>();
}
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 ---
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(
"Server=(localdb)\\mssqllocaldb;Database=DigitalShop_Identity;Trusted_Connection=True;MultipleActiveResultSets=true");
}