I am trying to create database , but after `context.Database.Migrate(); Database creates without problem, but always no tables are created except _EFMigrationHistory (empty). So may you help me to find source of problem or advice how to continue investigation?
There is my dbcontext:
public class CatalogDBContext : DbContext
{
public DbSet<CatalogItem> CatalogItems { get; set; }
public DbSet<CatalogBrand> CatalogBrands { get; set; }
public DbSet<CatalogType> CatalogTypes { get; set; }
public CatalogDBContext(DbContextOptions<CatalogDBContext> options) : base(options)
{
DbInterception.Add(new CLoggerEFInterceptor());
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new BrandEntityTypeConfiguration());
builder.ApplyConfiguration(new TypeEntityTypeConfiguration());
builder.ApplyConfiguration(new ItemEntityTypeConfiguration());
}
}
Program:
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<CatalogDBContext>();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
var settings = scope.ServiceProvider.GetRequiredService<IOptions<CatalogSettings>>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<CatalogDBSeedData>>();
context.Database.Migrate();
new CatalogDBSeedData().SeedAsync(context, env, settings, logger).Wait();
}
And migration:
public partial class InitialMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateSequence(
name: "brand_hilo",
incrementBy: 10);
migrationBuilder.CreateSequence(
name: "item_hilo",
incrementBy: 10);
migrationBuilder.CreateSequence(
name: "type_hilo",
incrementBy: 10);
migrationBuilder.CreateTable(
name: "Brand",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Brand", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Type",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Type", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Item",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false),
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
PictureFileName = table.Column<string>(type: "nvarchar(max)", nullable: true),
TypeId = table.Column<long>(type: "bigint", nullable: false),
BrandId = table.Column<long>(type: "bigint", nullable: false),
AvailableStock = table.Column<int>(type: "int", nullable: false),
RestockThreshold = table.Column<int>(type: "int", nullable: false),
MaxStockThreshold = table.Column<int>(type: "int", nullable: false),
OnReorder = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Item", x => x.Id);
table.ForeignKey(
name: "FK_Item_Brand_BrandId",
column: x => x.BrandId,
principalTable: "Brand",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Item_Type_TypeId",
column: x => x.TypeId,
principalTable: "Type",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Item_BrandId",
table: "Item",
column: "BrandId");
migrationBuilder.CreateIndex(
name: "IX_Item_TypeId",
table: "Item",
column: "TypeId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Item");
migrationBuilder.DropTable(
name: "Brand");
migrationBuilder.DropTable(
name: "Type");
migrationBuilder.DropSequence(
name: "brand_hilo");
migrationBuilder.DropSequence(
name: "item_hilo");
migrationBuilder.DropSequence(
name: "type_hilo");
}
}
CodePudding user response:
Try adding:
base.OnModelCreating(builder);
into your OnModelCreating
method
I've got similar situation months ago and this solved problem for me