Entity Framework 多重级联删除
Entity Framework Multiple Cascading Delete
我定义了三个模型,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
可以通过多种方式从级联中删除,但是,这是预期的行为。
如何让迁移工具生成不会引发此错误的迁移?
谢谢!
我最终改变了我的 类 使它们更具限制性,在这种情况下实际上效果更好。我删除了 PlayerRoom
对象并将 Room
引用移到了用户对象上。
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; }
public bool? isHost { get; set; }
[JsonIgnore]
public int? RoomId { get; set; }
public virtual Room Room { get; set; }
[JsonIgnore]
public bool permanent { get; set; }
}
public class Room
{
public int id { get; set; }
public string RoomName { get; set; }
}
通过将 Room
移动到用户而不是单独的对象上,它限制用户只能在一个 Room
中并消除我的级联删除问题
gets rid of my cascading delete issue
EF 使用 Code First 将级联设置为默认打开。要从模型中关闭它,可以策略性地将“WillCascadeOnDelete”置于相关实体之外:
.WillCascadeOnDelete(false);
或全球
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
我定义了三个模型,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
可以通过多种方式从级联中删除,但是,这是预期的行为。
如何让迁移工具生成不会引发此错误的迁移?
谢谢!
我最终改变了我的 类 使它们更具限制性,在这种情况下实际上效果更好。我删除了 PlayerRoom
对象并将 Room
引用移到了用户对象上。
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; }
public bool? isHost { get; set; }
[JsonIgnore]
public int? RoomId { get; set; }
public virtual Room Room { get; set; }
[JsonIgnore]
public bool permanent { get; set; }
}
public class Room
{
public int id { get; set; }
public string RoomName { get; set; }
}
通过将 Room
移动到用户而不是单独的对象上,它限制用户只能在一个 Room
中并消除我的级联删除问题
gets rid of my cascading delete issue
EF 使用 Code First 将级联设置为默认打开。要从模型中关闭它,可以策略性地将“WillCascadeOnDelete”置于相关实体之外:
.WillCascadeOnDelete(false);
或全球
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();