我有三个型号,用户,房间和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
可以从级联删除多种方式,但是,这是预期的行为。
如何获得迁移工具以生成不会导致此错误的迁移?
谢谢!
你生成从数据库或代码第一种方法的实体? – Agalo
我首先使用代码 – nealruggles1