问题
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:
- Add a nullable foreign key
- Set valid values on the foreign key
- 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