我想要的是从:
public class SomeEntity
{
public int Id {get; set;}
//...
}
至:
public class SomeEntity
{
public int Id {get; set;}
public int OtherEntityId {get; set;}
[ForeignKey("OtherEntityId")]
public OtherEntity Other {get; set;}
//...
}
在数据库级别,我假设这样做的唯一方法是:
- 添加可以为空的外键
- 在外键上设置有效值
- 添加非空约束
有没有办法通过 EntityFramework 迁移来实现这一点?我正在寻找的是可以放入 Seed 方法或其他地方的代码,它会自动更新数据库。我知道我可以添加一个可为空的外键,但我想要的是一步添加一个不可为空的外键。