实体框架:如何解决“FOREIGN KEY约束可能导致周期或多个级联路径”? [英] Entity Framework: how to solve "FOREIGN KEY constraint may cause cycles or multiple cascade paths"?
问题描述
有很多关于这个问题的问题,但是我无法解决我的问题。
可以有一个请看看这个:
there are lots of questions about this problem, but I couldn't solve my case. can some one please take a look at this:
我有一个 Office
表,其中有一个 - 与医生
和秘书
表之间的许多关系。最后一个表都是从 Employee
表中派生的,该表与预定义的用户
表具有共享主键关系由 sqlmembershipprovider
创建。在用户
表和角色
表中,我没有任何手牌似乎有很多关系
I have an Office
table which has one-many relationship with Doctor
and Secretary
tables. Both of the last tables, are derived from Employee
table which has a shared-primary-key relationship with predefined Users
table created by sqlmembershipprovider
. It seems there is a many-many relationship between Users
table and Roles
table which I haven't any hand in it.
我的问题是在我的 Employee
表和...之间创建一个(零,一个) - (一个)关系那个用户
表,我以它们之间的共享主键关系结束,然后提出错误。 (有更好的解决方案吗?)
My problem was in creating a (zero,one) - (one) relationship between my Employee
table and that Users
table which I ended with a shared primary key relationship between them and the error raised, then. (Is there a better solution for that problem?)
这里是错误:
介绍FOREIGN KEY约束
'表$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他
FOREIGN KEY约束。无法创建约束。查看以前的
错误。
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.
此处是逆向工程后的代码和成员代码: / p>
here are my codes and membership codes after reverse engineering:
public class Office
{
public Office()
{
this.Doctors = new HashSet<Doctor>();
this.Secretaries = new HashSet<Secretary>();
}
[Key]
public System.Guid OfficeId { get; set; }
public virtual ICollection<Doctor> Doctors { get; set; }
public virtual ICollection<Secretary> Secretaries { get; set; }
}
public class Employee
{
[Key, ForeignKey("User")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public System.Guid Id { get; set; }
public string Name { get; set; }
[ForeignKey("Office")]
public System.Guid OfficeId { get; set; }
// shared primary key
public virtual aspnet_Users User { get; set; }
public virtual Office Office { get; set; }
}
public class Doctor :Employee
{
public Doctor()
{
this.Expertises = new HashSet<Expertise>();
}
//the rest..
public virtual ICollection<Expertise> Expertises { get; set; }
}
public class Secretary : Employee
{
// blah blah
}
public class aspnet_Users
{
public aspnet_Users()
{
this.aspnet_Roles = new List<aspnet_Roles>();
}
public System.Guid ApplicationId { get; set; }
public System.Guid UserId { get; set; }
//the rest..
public virtual aspnet_Applications aspnet_Applications { get; set; }
public virtual ICollection<aspnet_Roles> aspnet_Roles { get; set; }
}
public class aspnet_Roles
{
public aspnet_Roles()
{
this.aspnet_Users = new List<aspnet_Users>();
}
public System.Guid ApplicationId { get; set; }
public System.Guid RoleId { get; set; }
//the rest..
public virtual aspnet_Applications aspnet_Applications { get; set; }
public virtual ICollection<aspnet_Users> aspnet_Users { get; set; }
}
编辑: Users
表和应用程序
表之间有许多关系,也位于角色
和应用程序
。
and the relationships go deeper, there is a many-one relationship between Users
table and Applications
table, also between Roles
and Applications
too.
推荐答案
使用流畅的api指定错误消息建议的操作。
You can use the fluent api to specify the actions the error message suggests.
在您的上下文中:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<aspnet_UsersInRoles>().HasMany(i => i.Users).WithRequired().WillCascadeOnDelete(false);
}
请注意,您没有包含表aspnet_UsersInRoles的定义,因此这段代码可能不起作用。
Note that you have not included the definition for the table aspnet_UsersInRoles so this code may not work.
另一个选项是通过添加此
Another option is to remove all CASCADE DELETES by adding this
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
如果您需要更多有关配置流畅api关系的信息,我建议 http://msdn.microsoft.com/en-US/data/jj591620
If you need more info about configuring relationships with the fluent api I suggest http://msdn.microsoft.com/en-US/data/jj591620
这篇关于实体框架:如何解决“FOREIGN KEY约束可能导致周期或多个级联路径”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!