我正在编写一个简单的 MVC 5 互联网应用程序。这是我的模型:
public class House
{
[Key]
public int Id { get; set; }
public string name { get; set; }
public virtual ICollection<Room> rooms { get; set; }
public int itemId { get; set; }
public virtual Item item { get; set; }
public House()
{
rooms = new List<Room>();
}
}
public class Room
{
[Key]
public int Id { get; set; }
public int roomNumber { get; set; }
public int houseId { get; set; }
public virtual House house { get; set; }
public int itemId { get; set; }
public virtual Item item { get; set; }
}
public class Item
{
[Key]
public int Id { get; set; }
public string name { get; set; }
}
当我在 House 控制器中加载 Index 操作结果时,我收到此错误:
Introducing FOREIGN KEY constraint 'FK_dbo.Rooms_dbo.Items_itemId' on table 'Rooms' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
我可以请一些帮助来正确编码吗?
提前致谢
编辑
我现在已将ForeignKey
属性添加到字段外键字段:
public class House
{
[Key]
public int Id { get; set; }
public string name { get; set; }
public virtual ICollection<Room> rooms { get; set; }
public int itemId { get; set; }
[ForeignKey("itemId")]
public virtual Item item { get; set; }
public House()
{
rooms = new List<Room>();
}
}
public class Room
{
[Key]
public int Id { get; set; }
public int roomNumber { get; set; }
public int houseId { get; set; }
[ForeignKey("houseId")]
public virtual House house { get; set; }
public int itemId { get; set; }
[ForeignKey("itemId")]
public virtual Item item { get; set; }
}
public class Item
{
[Key]
public int Id { get; set; }
public string name { get; set; }
}
我仍然遇到同样的错误。