我创建了这个表:
public string Name { get; set; }
public string Description { get; set; }
[ForeignKey("CityId")]
public City City { get; set; }
public int CityId { get; set; }
现在我想将CityId设置为null,我试试这个:
public string Name { get; set; }
public string Description { get; set; }
[ForeignKey("CityId")]
public City City { get; set; }
public int? CityId { get; set; }
但是当我在Package Manager Console中创建新的迁移(add-migration Curso_CityIdNullable)时,生成的迁移是空的......
public override void Up()
{
}
public override void Down()
{
}
任何帮助?感谢
答案 0 :(得分:1)
您的更改未将CityId设置为null。它让它可以为空!那不一样。你想要什么,将行中的CityId设置为null或将字段设置为可空?
您可以在此处找到ef的数据注释列表:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx