实体框架:如何解决“FOREIGN KEY 约束可能导致循环或多个级联路径"? [英] Entity Framework: how to solve "FOREIGN KEY constraint may cause cycles or multiple cascade paths"?

查看:27
本文介绍了实体框架:如何解决“FOREIGN KEY 约束可能导致循环或多个级联路径"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个问题有很多问题,但我无法解决我的问题.有人可以看看这个:

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 表,它与 DoctorSecretary 表有一对多的关系.最后两个表都源自 Employee 表,该表与由 sqlmembershipprovider 创建的预定义 Users 表具有共享主键关系.Users 表和 Roles 表之间似乎存在多对多关系,而我没有参与其中.

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 表和我以共享主键关系结束的 Users 表之间创建(零,一)-(一)关系在他们和引发的错误之间,然后.(这个问题有没有更好的解决方案?)

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 约束表上的FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId"'aspnet_UsersInRoles' 可能会导致循环或多个级联路径.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他外键约束.无法创建约束.见上一个错误.

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.

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

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 表和 Applications 表之间存在多对关系,角色应用程序也是如此.

and the relationships go deeper, there is a many-one relationship between Users table and Applications table, also between Roles and Applications too.

推荐答案

您可以使用 fluent 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.

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

Another option is to remove all CASCADE DELETES by adding this

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

如果您需要有关使用 fluent 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆