在迁移之前,我在表Work和Factory之间有一对多的关系:
public class Work
{
public int WorkId { get; set; }
public string Name { get; set; }
public virtual List<Factory> MyFactory { get; set; }
}
和
public class Factory
{
public int FactoryId { get; set; }
public string Name { get; set; }
public virtual Work ParentWork { get; set; }
}
然后我尝试将关系改为1比1:
public class Work
{
public int WorkId { get; set; }
public string Name { get; set; }
[Required]
public virtual Factory MyFactory { get; set; }
}
public class Factory
{
public int FactoryId { get; set; }
public string Name { get; set; }
public virtual Work ParentWork { get; set; }
}
当我尝试使用以下命令创建迁移时:
添加迁移MyMigration
我收到以下错误:
ALTER TABLE语句与FOREIGN KEY约束冲突&#34; FK_dbo.Work_dbo.Factory_WorkId&#34;。冲突发生在数据库&#34; EntityFrameworkLab.MyContext&#34;,table&#34; dbo.Factory&#34;,column&#39; FactoryId&#39;
现在,我有几个问题:
1)为什么无法创建迁移,而模型是否有效?
2)我应该在代码中实现哪些更改?
3)如果我尝试手动还原更改,则会出现相同的错误消息,但这次我尝试执行此操作时:
update-database -verbose