C#实体框架核心迁移的最佳实践



我有一个小程序,它使用EFCore 5.0.12。我还在程序中添加了一个公司和个人模型,这是我的DB模型。然后我添加了一个迁移来初始创建表。

然后,我为Person模型添加了一个Address模型,它也应该是一个DB模型。进行了第二次迁移,您可以在下面看到。但是,如果我再次运行我的代码,它似乎想要迁移这两个迁移。

如何从特定迁移中迁移数据库。

这是我的代码:

这是我的TestDbContext,它应该做所有的Db工作。

public class TestDbContext : DbContext
{
public DbSet<Company> Companies { get; set; }
private string _dbPath = Path.Combine(".", "TestDb.sqlite");
public TestDbContext() 
{
if (File.Exists(_dbPath))
Database.Migrate();
else Database.EnsureCreated();
}
protected override void OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder optionsBuilder)
{
if (!Directory.Exists(Path.GetDirectoryName(_dbPath)))
Directory.CreateDirectory(Path.GetDirectoryName(_dbPath));
try
{
optionsBuilder.UseSqlite($"Data Source={Path.GetFullPath(_dbPath)}");
}
catch (Exception ex)
{
//Log.Error("Could not create DB");
//Log.Error(ex);
return;
}
}

此外,我还得到了这个公司模型,它继承了BaseEntry,BaseEntry将ID字段保存为

public class Company : BaseEntry
{
public List<Person> Employees { get; set; } = new List<Person>();
}

Person模型也从BaseEntry继承以获得PrimaryKey。

public class Person : BaseEntry
{
public string Name { get; set; }
public string Firstname { get; set; }
public Address Address { get; set; }
}

迁移时应添加的地址。

public class Address : BaseEntry
{
public string Street { get; set; }
public uint Number { get; set; }
public string City { get; set; }
}

以及保存ID字段的BaseEntry。

public abstract class BaseEntry
{
[Key]
public ulong Id { get; set; }
}

这是第一次迁移,它确保了表内容

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Companies",
columns: table => new
{
Id = table.Column<ulong>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true)
},
constraints: table =>
{
table.PrimaryKey("PK_Companies", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Person",
columns: table => new
{
Id = table.Column<ulong>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: true),
Firstname = table.Column<string>(type: "TEXT", nullable: true),
CompanyId = table.Column<ulong>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Person", x => x.Id);
table.ForeignKey(
name: "FK_Person_Companies_CompanyId",
column: x => x.CompanyId,
principalTable: "Companies",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Person_CompanyId",
table: "Person",
column: "CompanyId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Person");
migrationBuilder.DropTable(
name: "Companies");
}
}

然后是第二次迁移,它将Adress内容添加到DB:

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "AddressId",
table: "Person",
type: "INTEGER",
nullable: true);
migrationBuilder.CreateTable(
name: "Address",
columns: table => new
{
Id = table.Column<ulong>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Street = table.Column<string>(type: "TEXT", nullable: true),
Number = table.Column<uint>(type: "INTEGER", nullable: false),
City = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Address", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Person_AddressId",
table: "Person",
column: "AddressId");
migrationBuilder.AddForeignKey(
name: "FK_Person_Address_AddressId",
table: "Person",
column: "AddressId",
principalTable: "Address",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Person_Address_AddressId",
table: "Person");
migrationBuilder.DropTable(
name: "Address");
migrationBuilder.DropIndex(
name: "IX_Person_AddressId",
table: "Person");
migrationBuilder.DropColumn(
name: "AddressId",
table: "Person");
}
}

但是,由于第一次迁移由于表已存在。我的主要问题是,如何说EFCore数据库已经迁移到特定的点,它应该只从这个点迁移?

如果您想告诉EF您的第一次迁移已经迁移。您可以手动将其添加到迁移历史表中默认表为__EFMigrationsHistory

MigrationId 产品版本
2021061200000_initialize_lah_blah 5.0.1.2
your_migration_id 版本

最新更新