c# - EF Core 3.1.8 - 迁移错误 - 尚未为此 DbContext 配置数据库提供程序

标签 c# asp.net-core .net-core entity-framework-core

我尝试添加迁移并收到以下错误:

Build started...
Build succeeded.
System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

已尝试添加 EntityFrameworkCore.Design包和我的构造函数就像

public MyContext(DbContextOptions<MyContext> options) : base(options)
{

}

并继续抛出错误:

Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

更新了数据库配置

观测数据:

  1. User 是一个抽象类。
  2. 我首先使用代码,并且迁移尚不存在。
public class MyContext : DbContext
{
    public VenturaContext(DbContextOptions<MyContext> options) : base(options)
    {

    }

    public DbSet<Usuario> Usuarios { get; set; }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Admin> Administrators { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //modelBuilder.Entity<User>().ToTable("Users");
        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    }
}

[更新 2 - 我的简单解决方案]

只需在 OnConfiguring 方法上设置我的 conn 字符串,并在 MyContext 上创建一个空构造函数。对于正在创建的内容来说这已经足够了。

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Server=KONOHA; Database=my_database; User Id=user; password=********; MultipleActiveResultSets=true");
            }

            base.OnConfiguring(optionsBuilder);
        }

最佳答案

您可以看到this文章。

通过AddDbContext配置DBContext:

services.AddDbContext<MyContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

然后在您的 MyContext 中:

public class MyContext : DbContext
{

public MyContext(DbContextOptions<MyContext> options) : base(options)
{
}  
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    
    base.OnModelCreating(modelBuilder);
}
}

appsettings.json:

  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyContext;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

关于c# - EF Core 3.1.8 - 迁移错误 - 尚未为此 DbContext 配置数据库提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64294741/

相关文章:

c# - 返回 .NET Core 中 DocumentDB 中新创建文档的 id

c# - Windows 事件日志问题?

c# - ASP.Net Core MVC - 自定义属性的客户端验证

c# - 不为同步请求调用中间件

c# - 将 NLog 与 MSTest (.NET Core 2.0) 结合使用

c# - 如何通过动态创建客户端代理来使用 WCF 服务

javascript - 防止页面刷新时显示模型窗口

c# - POST HttpRequestMessage 为空

c# - 在 appSettings.json 文件中展开环境变量

c# - 如何降低 dockerized .Net Core 应用程序的大小?