【发布时间】:2015-07-19 18:39:54
【问题描述】:
我定义了三个模型,User、Room 和 PlayerRoom:
public class User
{
public int id { get; set; }
public string UserName { get; set; }
//flags user to be deleted when room is no longer available
public bool temporaryUser { get; set; }
[JsonIgnore]
public bool permanent { get; set; }
}
public class Room
{
public int id { get; set; }
public string RoomName { get; set; }
[JsonIgnore]
public int CreatedById { get; set; }
public virtual User CreatedBy { get; set; }
}
public class PlayerRoom
{
public int id { get; set; }
[JsonIgnore]
public int RoomId { get; set; }
public virtual Room Room { get; set; }
[JsonIgnore]
public int UserId { get; set; }
public virtual User User { get; set; }
}
我想要完成的是设置模型,以便当 User 被删除或 Room 被删除时,所有关联的 PlayerRoom 都会被删除。
目前,当我生成迁移并运行 update-database 时,我收到错误:
Introducing FOREIGN KEY constraint 'FK_dbo.Rooms_dbo.Users_CreatedById' 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.
无法创建约束。查看以前的错误。
根据我所做的研究,这是因为PlayerRoom 可以通过多种方式从级联中删除,但这是预期的行为。
如何让迁移工具生成不会抛出此错误的迁移?
谢谢!
【问题讨论】:
-
您是从数据库还是代码优先的方法生成实体?
-
我先用代码
标签: c# .net entity-framework entity-framework-migrations