我有一个包含我的 ApplicationDbContext 类的 Persistence 项目,我希望在这个项目中创建迁移。
Persistence.Context.DbContext
public class PizzaDeliveryDbContext : DbContext
{
private readonly IConfiguration _config;
public PizzaDeliveryDbContext(IConfiguration config)
{
_config = config;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_config.GetConnectionString("pizza"));
}
// each dbset maps to a table that will be created in the database
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
}
在我的启动项目中(与 Persistence 分开)
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("pizza");
// register dbcontext
builder.Services.AddDbContext<PizzaDeliveryDbContext>(options =>
options.UseNpgsql(connectionString));
var app = builder.Build();
当我尝试运行
dotnet ef migrations add InitialCreate
(指向持久目录)时-我收到以下错误:
/user Persistence % dotnet ef migrations add InitialCreate
Build started...
Build succeeded.
Unable to create an object of type 'PizzaDeliveryDbContext'. For
the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Running dotnet ef migrations add InitialCreate -verbose
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Persistence'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'PizzaDeliveryDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'PizzaDeliveryDbContext'.
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'Persistence.Context.PizzaDeliveryDbContext'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
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, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
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)
您不必从配置中获取连接字符串,因为您已经将它添加到
databasecontext
选项提供程序。除此之外,您没有初始化 DbContext
的新实例
您必须添加
: base(options)
,以创建一个新实例,该实例也将能够获取您在startup.cs
中指定的连接字符串。
替换
private readonly IConfiguration _config;
public PizzaDeliveryDbContext(IConfiguration config)
{
_config = config;
}
protected override void OnConfiguring(DbContextOptionsBuilderoptionsBuilder)
{
optionsBuilder.UseNpgsql(_config.GetConnectionString("pizza"));
}
与
public PizzaDeliveryDbContext(DbContextOptions<PizzaDeliveryDbContext> options) : base(options)
{
}