我有一个持久性项目,其中包括我的ApplicationDbContext类,我希望在这个项目中创建迁移。
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; }
}
在我的启动项目中(与持久性分开)
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'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Persistence.Context.PizzaDeliveryDbContext]' 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)
Unable to create an object of type 'PizzaDeliveryDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
我安装的持久化项目:Microsoft.EntityFrameworkCore 7.0.5, Microsoft.EntityFrameworkCore.Design 7.0.5, Microsoft.EntityFrameworkCore.Tools 7.0.5, Npgsql.EntityFrameworkCore.PostreSQL 7.0.3
1条答案
按热度按时间ffscu2ro1#
我使用的是visual studio for mac,所以我发现在运行Add-migration时,它似乎认为我的迁移项目和启动项目是相同的。
当然,情况并非如此,所以我必须像这样指定项目路径。
dotnet ef migrations add InitialCreate --project ../Persistence --startup-project ../pizza-delivery