asp.net – 实体框架:如何解决“FOREIGN KEY约束可能导致循环或多个级联路径”?

前端之家收集整理的这篇文章主要介绍了asp.net – 实体框架:如何解决“FOREIGN KEY约束可能导致循环或多个级联路径”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有很多关于这个问题的问题,但我不能解决我的情况。
可以一些请看看这个:

我有一个Office表与医生和秘书表有一对多的关系。两个最后的表都是从Employee表派生的,它与sqlmembershipprovider创建的预定义Users表具有共享主键关系。看起来在Users表和Roles表之间有许多关系,我没有任何手。

我的问题是在我的Employee表和用户表之间创建一个(零,一) – (一)关系,我结束了它们之间的共享主键关系和提出的错误。 (有问题的更好的解决方案吗?)

这里是错误

Introducing FOREIGN KEY constraint
‘FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId’ on table
‘aspnet_UsersInRoles’ 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. See prevIoUs
errors.

这里是我的代码和成员代码后逆向工程:

  1. public class Office
  2. {
  3. public Office()
  4. {
  5. this.Doctors = new HashSet<Doctor>();
  6. this.Secretaries = new HashSet<Secretary>();
  7. }
  8.  
  9. [Key]
  10. public System.Guid OfficeId { get; set; }
  11. public virtual ICollection<Doctor> Doctors { get; set; }
  12. public virtual ICollection<Secretary> Secretaries { get; set; }
  13. }
  14.  
  15. public class Employee
  16. {
  17. [Key,ForeignKey("User")]
  18. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  19. public System.Guid Id { get; set; }
  20. public string Name { get; set; }
  21.  
  22. [ForeignKey("Office")]
  23. public System.Guid OfficeId { get; set; }
  24.  
  25. // shared primary key
  26. public virtual aspnet_Users User { get; set; }
  27.  
  28. public virtual Office Office { get; set; }
  29. }
  30.  
  31. public class Doctor :Employee
  32. {
  33. public Doctor()
  34. {
  35. this.Expertises = new HashSet<Expertise>();
  36. }
  37. //the rest..
  38. public virtual ICollection<Expertise> Expertises { get; set; }
  39. }
  40.  
  41. public class Secretary : Employee
  42. {
  43. // blah blah
  44. }
  45.  
  46. public class aspnet_Users
  47. {
  48. public aspnet_Users()
  49. {
  50. this.aspnet_Roles = new List<aspnet_Roles>();
  51. }
  52.  
  53. public System.Guid ApplicationId { get; set; }
  54. public System.Guid UserId { get; set; }
  55. //the rest..
  56. public virtual aspnet_Applications aspnet_Applications { get; set; }
  57. public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
  58. }
  59.  
  60. public class aspnet_Roles
  61. {
  62. public aspnet_Roles()
  63. {
  64. this.aspnet_Users = new List<aspnet_Users>();
  65. }
  66.  
  67. public System.Guid ApplicationId { get; set; }
  68. public System.Guid RoleId { get; set; }
  69. //the rest..
  70. public virtual aspnet_Applications aspnet_Applications { get; set; }
  71. public virtual ICollection<aspnet_Users> aspnet_Users { get; set; }
  72. }

编辑:和关系更深,在用户表和应用程序表之间有一个多对一的关系,也在角色和应用程序之间。

解决方法

您可以使用fluent api指定错误消息建议的操作。

在您的上下文中:

  1. protected override void OnModelCreating( DbModelBuilder modelBuilder )
  2. {
  3. base.OnModelCreating(modelBuilder);
  4. modelBuilder.Entity<aspnet_UsersInRoles>().HasMany(i => i.Users).Withrequired().WillCascadeOnDelete(false);
  5. }

请注意,您没有包括aspnet_UsersInRoles表的定义,因此此代码可能无法正常工作。

另一个选项是通过添加删除所有CASCADE DELETES

  1. modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

如果你需要更多的信息关于配置关系与流利的API,建议http://msdn.microsoft.com/en-US/data/jj591620

猜你在找的asp.Net相关文章