我有一個使用物體框架的 .NET Core 5 Web API。WebAPI 在一個專案 GestionStock.API 中,而 EF 與另一個專案 GestionStock.Infrastructure 中的 DBContext 類

我想通過運行命令來創建遷移腳本
Add-Migration Initiale
不幸的是我得到了錯誤
Unable to create an object of type 'GestionStockContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?
類 DbContext
public class GestionStockContext : DbContext
{
public GestionStockContext(DbContextOptions<GestionStockContext> options) : base(options)
{
}
public DbSet<Client> Clients { get; set; }
public DbSet<Commande> Commandes { get; set; }
public DbSet<LignesCommandes> LignesCommandes { get; set; }
public DbSet<Produit> Produits { get; set; }
//Pour mettre des données par défault au début
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
Startup.cs WebApi 專案中的服務方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "GestionStock.API", Version = "v1" });
});
services.AddDbContext<GestionStockContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString(
"Default"),x => x.MigrationsAssembly("GestionStock.Infrastructure"))
);
}
uj5u.com熱心網友回復:
您可以使用該-startup-project選項來指定 API 專案。請參閱此處的檔案:https : //docs.microsoft.com/en-us/ef/core/cli/dotnet#using-the-tools
就像是:
dotnet ef migrations add MyMigrationName --startup-project GestionStock.API
uj5u.com熱心網友回復:
您可以設定啟動和/或 DbContext 專案的相對路徑;如果未設定,它們都將默認為當前檔案夾,只有在它們位于公共專案中時才有效。
選項:
--project(-p)--startup-project(-s)
用法:
dotnet ef migrations add MyMigration [-p <relative path to DbContext project>, -s <relative path to startup project>]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346739.html
