实体框架:如何解决" 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:
我有与博士
表。在最后一个表,从秘书长一对多的关系
和办公室
表员工
表,有predefined 用户共享主键关系衍生
按的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.
我的问题是在创造一个(零一) - 我的员工
表之间的用户$ C $(一个)的关系C>表,我与他们养大,然后错误之间的共享主键的关系结束了。 (有没有这个问题更好的解决方案?)
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?)
这里是错误:
引进国外KEY约束
对表'FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId
aspnet_UsersInRoles'可能会导致循环或多重级联路径。
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他
FOREIGN KEY约束。无法创建约束。见previous
错误。
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.
这里有我的codeS和会员codeS逆向工程后:
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; }
}
编辑:并关系走向深入,有用户
表和应用程序之间的许多一对一的关系
表中,也角色之间
和应用
了。
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所以这code可能无法正常工作的定义。
Note that you have not included the definition for the table aspnet_UsersInRoles so this code may not work.
另一种方法是删除所有CASCADE加入这将删除
Another option is to remove all CASCADE DELETES by adding this
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
如果您需要了解与流畅的API的配置关系的详细信息,我建议<一href=\"http://msdn.microsoft.com/en-US/data/jj591620\">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
这篇关于实体框架:如何解决&QUOT; FOREIGN KEY约束可能会导致循环或多重级联路径&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!