我使用的是EF 6.1.3。
这是我最初的架构:
[Table("Children")]
public class Child
{
[Key]
public int Id { get; set; }
public virtual Parent Parent { get; set; }
}
[Table("Parents")]
public class Parent
{
[Key]
public int Id { get; set; }
[InverseProperty("Parent")]
public virtual ICollection<Child> Children { get; set; }
}
当我将关联的多样性更改为(我使用edmx执行此IRL时,但此示例说明了问题):
[Table("Children")]
public class Child
{
[Key]
public int Id { get; set; }
[Required]
public virtual Parent Parent { get; set; }
}
[Table("Parents")]
public class Parent
{
[Key]
public int Id { get; set; }
public virtual Child Child { get; set; }
}
据我所知,唯一必要的数据库架构更改是Children.Parent列的可为空性。然而,EF产生了这种疯狂:
public override void Up()
{
DropIndex("dbo.Children", new[] { "Parent_Id" });
DropColumn("dbo.Children", "Id");
RenameColumn(table: "dbo.Children", name: "Parent_Id", newName: "Id");
DropPrimaryKey("dbo.Children");
AlterColumn("dbo.Children", "Id", c => c.Int(nullable: false));
AlterColumn("dbo.Children", "Id", c => c.Int(nullable: false));
AddPrimaryKey("dbo.Children", "Id");
CreateIndex("dbo.Children", "Id");
}
这显然毫无意义。
WTF正在这里?