简体   繁体   English

即使级联删除已关闭,也会获得“外键约束”

[英]Getting “Foreign Key constraint” even though cascade delete is turned off

I have the following user model: 我有以下用户模型:

 public class User
    {
        public Guid Id { get; set; }
        [ForeignKey("Location")]
        public Guid LocationId { get; set; }
        public Location Location { get; set; }
        [ForeignKey("MainPhoneNumber")]
        public Guid? MainPhoneNumberId { get; set; }
        [ForeignKey("Mailbox")]
        public Guid? MailboxId { get; set; }
        [ForeignKey("Fax")]
        public Guid? FaxId { get; set; }

        [MaxLength(50)]
        public string UserName { get; set; }
        [ForeignKey("Conference")]
        public Guid? ConferenceId { get; set; }

        public virtual PhoneNumber MainPhoneNumber { get; set; }
        public virtual PhoneNumber Mailbox { get; set; }
        public virtual PhoneNumber Fax { get; set; }
        public virtual PhoneNumber Conference { get; set; }

        public virtual ICollection<AddOn> AddOns { get; set; }
    }

I also added the following rules using Fluent Api: 我还使用Fluent Api添加了以下规则:

modelBuilder.Entity<User>()
                           .HasRequired(usr=>usr.Location)
                           .WithMany()
                           .WillCascadeOnDelete(false);

            modelBuilder.Entity<User>()
                        .HasOptional(usr => usr.Fax)
                        .WithMany()
                        .WillCascadeOnDelete(false);
            modelBuilder.Entity<User>()
                      .HasOptional(usr => usr.Conference)
                      .WithMany()
                      .WillCascadeOnDelete(false);
            modelBuilder.Entity<User>()
                      .HasOptional(usr => usr.Mailbox)
                      .WithMany()
                      .WillCascadeOnDelete(false);
            modelBuilder.Entity<User>()
                      .HasOptional(usr => usr.MainPhoneNumber)
                      .WithMany()
                      .WillCascadeOnDelete(false);

I would expect that these fluent rules will help me with any Cascade delete - problems. 我希望这些流利的规则可以帮助我解决所有Cascade删除问题。 But still I receive the following error when trying to run " Update-Database " inside the console: 但是尝试在控制台中运行“ Update-Database ”时仍然收到以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.User_dbo.PhoneNumber_FaxId' on table 'User' may cause cycles or multiple cascade paths. 在表'User'上引入FOREIGN KEY约束'FK_dbo.User_dbo.PhoneNumber_FaxId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。

Note: Please be aware that this is NOT a duplicate from that question : Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? 注意:请注意, 这不是该问题的重复项引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么?

As you can see: The suggestions made there in the most topvoted answer are already included in my code (Fluent part) 如您所见:在最高投票答案中提出的建议已包含在我的代码中(流利的部分)

(Update) THis is the PhoneNumber Class: (更新)这是PhoneNumber类:

public class PhoneNumber
    {
            public Guid Id { get; set; }
            [ForeignKey("PhoneNumberPool")]
            public Guid PhoneNumberPoolId { get; set; }
            public virtual PhoneNumberPool PhoneNumberPool { get; set; }

            public int Length { get; set; }     

        }

PhoneNumberPool itself has a few simple properties (string, int, etc.) and a reference to "Location" (as well as the User). PhoneNumberPool本身具有一些简单属性(字符串,整数等)和对“位置”(以及用户)的引用。

will this datamodel work for you? 这个数据模型对您有用吗?

this way you can add new types of phone numbers 这样您可以添加新的电话号码类型

    public enum PhoneNumberType {Main, Mailbox, Fax, Conference, Other}


PhoneNumber{
     public PhoneNumberType  NumberType{get;set;};
     public string Number{get;set;};
    //other properties

    public virtual User{get;set;}
    }

    public class User
        {
            public Guid Id { get; set; }
            [ForeignKey("Location")]
            public Guid LocationId { get; set; }
            public Location Location { get; set; }
            [MaxLength(50)]
            public string UserName { get; set; }
            public virtual List<PhoneNumber> PhoneNumbers { get; set; }
            public virtual ICollection<AddOn> AddOns { get; set; }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使指定了ON DELETE CASCADE,DELETE也会失败并显示“ Foreign Key约束失败” - DELETE fails with “foreign key constraint failed” even though ON DELETE CASCADE is specified 实体框架级联删除 - FOREIGN KEY约束 - Entity Framework Cascade delete - FOREIGN KEY constraint EF Core 将外键约束设置为“ON DELETE RESTRICT”,即使 FK 可为空 - EF Core sets the Foreign Key constraint as 'ON DELETE RESTRICT' even though the FK is nullable 实体框架核心,由于级联删除导致的外键约束 - Entity Framework core, foreign key constraint due to cascade delete 即使值已经存在,外键约束问题 - FOREIGN KEY constraint issue even though value already exists 级联删除/可空外键 - Cascade on Delete/Nullable Foreign Key 可以为空的外键上的级联删除 - cascade delete on nullable foreign key 删除具有外键约束的记录 - Delete a record with a foreign key constraint 在Code First中对外键进行级联删除 - Cascade delete on a foreign key in Code First 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM