EF 6.3:它创建相同的迁移以从 Data Annotation 更改为 Fluent API
EF 6.3: It creates the same migration for changing from DataAnnotation to FluentAPI
我有数据标注:
[Required]
[MaxLength(150)]
[Index(IsUnique = true)]
public string GuidName { get; set; }
现在我们需要将其移动到 Fluent API(不要问我为什么)。
我的代码:
this.Property(c => c.GuidName).IsRequired().HasMaxLength(150);
this.HasIndex(c => c.GuidName).IsUnique(true).IsClustered(false);
它生成以下迁移:
public override void Up()
{
DropIndex("dbo.Companies", new[] { "CompanyUniqueString" });
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
}
public override void Down()
{
DropIndex("dbo.Companies", new[] { "CompanyUniqueString" });
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
}
正如我们所见,它在 Up 和 Down 中做了相同的事情并且具有相同的代码。但是为什么它会生成呢?
您已从字段中删除了 Index
数据注释,这就是为什么在 Up()
方法中生成 DropIndex(...)
行并在 Down()
方法。同时,您通过 Fluent API 添加了索引,它为您提供了其余部分(Up()
方法中的 CreateIndex(...)
和 Down()
中的 DropIndex(...)
) .
因此,EF 检测到模型中的两个更改,并且不检查 Fluent API 是否生成与删除的数据注释完全相同的索引。
我有数据标注:
[Required]
[MaxLength(150)]
[Index(IsUnique = true)]
public string GuidName { get; set; }
现在我们需要将其移动到 Fluent API(不要问我为什么)。 我的代码:
this.Property(c => c.GuidName).IsRequired().HasMaxLength(150);
this.HasIndex(c => c.GuidName).IsUnique(true).IsClustered(false);
它生成以下迁移:
public override void Up()
{
DropIndex("dbo.Companies", new[] { "CompanyUniqueString" });
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
}
public override void Down()
{
DropIndex("dbo.Companies", new[] { "CompanyUniqueString" });
CreateIndex("dbo.Companies", "CompanyUniqueString", unique: true);
}
正如我们所见,它在 Up 和 Down 中做了相同的事情并且具有相同的代码。但是为什么它会生成呢?
您已从字段中删除了 Index
数据注释,这就是为什么在 Up()
方法中生成 DropIndex(...)
行并在 Down()
方法。同时,您通过 Fluent API 添加了索引,它为您提供了其余部分(Up()
方法中的 CreateIndex(...)
和 Down()
中的 DropIndex(...)
) .
因此,EF 检测到模型中的两个更改,并且不检查 Fluent API 是否生成与删除的数据注释完全相同的索引。