簡體   English   中英

EF Core 3.1.8 - 遷移錯誤 - 沒有為此 DbContext 配置數據庫提供程序

[英]EF Core 3.1.8 - Migrations Error - No database provider has been configured for this DbContext

我正在嘗試添加遷移並收到以下錯誤:

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 package 並且我的構造函數就像

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

}

並繼續拋出錯誤:

無法創建“MyContext”類型的 object。 有關設計時支持的不同模式,請參閱https://go.microsoft.com/fwlink/?linkid=851728

更新了數據庫配置

觀察:

  1. 用戶是抽象的 class。
  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 方法上設置我的連接字符串,並在 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);
        }

你可以看看這篇文章。

通過 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);
}
}

應用設置.json:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM