I have a .NET Core 5 Web API using Entity Framework. The WebAPI is in a project GestionStock.API, whereas the EF with the DBContext classes in another project GestionStock.Infrastructure
I want to create the migration script by running command
Add-Migration Initiale
Unfortunately I get the error
Unable to create an object of type 'GestionStockContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?
Class 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);
}
}
Service method in Startup.cs WebApi Project
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"))
);
}
CodePudding user response:
You can use the -startup-project
option to specify the API project.
See the documentation here: https://docs.microsoft.com/en-us/ef/core/cli/dotnet#using-the-tools
Something like:
dotnet ef migrations add MyMigrationName --startup-project GestionStock.API
CodePudding user response:
You can set the relative path to the startup and/or the DbContext project; left unset, they both will default to the current folder which only works if they're in a common project.
--project
(-p
)--startup-project
(-s
)
Usage:
dotnet ef migrations add MyMigration [-p <relative path to DbContext project>, -s <relative path to startup project>]