Does EF Migrations Support Adding Not Null Foreign Keys

人盡茶涼 提交于 2019-12-11 03:41:07

问题


What I'd like is to go from:

public class SomeEntity
{
    public int Id {get; set;}

    //...
}

To:

public class SomeEntity
{
    public int Id {get; set;}

    public int OtherEntityId {get; set;}

    [ForeignKey("OtherEntityId")]
    public OtherEntity Other {get; set;}

    //...
}

At the database level, I'm assuming the only way to do this is:

  1. Add a nullable foreign key
  2. Set valid values on the foreign key
  3. Add a not-null constraint

Is there some way to make this happen with EntityFramework migrations? What I'm looking for is code that could be put into the Seed method, or elsewhere, that would update the database automatically. I understand that I can add a nullable foreign key, but what I want is to add a non-nullable foreign key in one step.

来源:https://stackoverflow.com/questions/18494747/does-ef-migrations-support-adding-not-null-foreign-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!