![](/img/trans.png)
[英]When migrating from EF to EF Core do you need to create a new migration with the entire database structure?
[英]EF Core 5.0 - Do you need to generate a migration when changing a “Defining Query” -mapped entity?
I added an entity class to my EF Core 5.0/MS SQL Server data model that is backed by a defining query (raw SQL query, with no table or view corresponding to it in my database).
當我對其進行更改(例如,添加新列),然后在 Package 管理器控制台中運行Add-Migration
以創建遷移步驟時,它會生成一個帶有空Up(MigrationBuilder migrationBuilder)
和Down(MigrationBuilder migrationBuilder)
方法的遷移。 但是生成的 [MigrationName].Designer.cs 文件包含新列,並且我的 DbContext 的 ModelSnapshot 被修改為包含新列,因此發生了一些變化。
我的問題是,每次對這些實體之一進行更改時,是否需要添加遷移才能使我的應用程序正確地轉到 function? 如果不需要它們,當我更新定義查詢支持的實體時,什么被認為是更好的做法:
A. 添加這些遷移,即使它們有空白的Up(MigrationBuilder migrationBuilder)
和Down(MigrationBuilder migrationBuilder)
方法,因為 model 已更改,或者
B. 不生成遷移,只是讓這些更改在下次有人進行實際影響底層數據庫結構的更改時被拾取?
代碼片段(過度簡化以刪除識別數據):
實體 class
public class Thing
{
public int Id { get; set; }
public string Name { get; set; }
}
實體配置:
public class ThingTypeConfiguration : IEntityTypeConfiguration<Thing>
{
public void Configure(EntityTypeBuilder<Thing> builder)
{
builder
.HasNoKey()
.ToView("Dummy") // This is here to work around an issue with the migrations auto-generating a table for the entity - see https://github.com/dotnet/efcore/issues/19972
.ToSqlQuery(
@"SELECT
[TableName].[IdColumn] AS Id,
[TableName].[OtherColumn] AS Name
FROM
[TableName]");
}
}
數據庫上下文:
public class MyDbContext : DbContext
{
public MyDbContext()
{
}
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public virtual DbSet<Thing> Things { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
}
}
生成的遷移部分類的示例:
部分 class 帶有空的 Up/Down 方法:
public partial class MyMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
自動生成的 __.Designer.cs 文件(類的 rest):
[DbContext(typeof(MyDbContext))]
[Migration("20210302175116_MyMigration")]
partial class MyMigration
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.3")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
/* Entity configuration logic for all the other entities in my model.. */
modelBuilder.Entity("MyDataProject.Thing", b =>
{
b.Property<string>("Id")
.HasColumnType("int");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.ToView("Dummy");
b
.HasAnnotation("Relational:SqlQuery", "/* My SQL query */");
});
#pragma warning restore 612, 618
}
}
否。如果數據庫架構沒有更改,則無需添加新遷移。
哎呀,如果新架構與舊架構兼容,您甚至不需要添加新的遷移。 例如,刪除可選屬性仍然是兼容的。
*.Designer 文件只是在生成 SQL 時偶爾提供有關 model 的附加信息。 而且,由於遷移不會生成 SQL,因此在這種情況下完全未使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.