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

时间:2015-07-06 14:17:29

标签: c# entity-framework

我有以下用户模型:

 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; }
    }

我还使用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);

我希望这些流畅的规则可以帮助我解决任何级联删除问题。但是在尝试在控制台中运行“Update-Database”时仍然收到以下错误:

  

介绍FOREIGN KEY约束'FK_dbo.User_dbo.PhoneNumber_FaxId'   在表'用户'可能会导致循环或多个级联路径。指定ON   DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY   限制。无法创建约束。查看以前的错误。

注意:请注意这不是该问题的重复Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

正如您所看到的那样:我在最受欢迎的答案中提出的建议已包含在我的代码中(流利部分)

(更新)这是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本身有一些简单的属性(字符串,整数等)和对“位置”(以及用户)的引用。

1 个答案:

答案 0 :(得分:-1)

这个数据模型会为你工作吗?

通过这种方式,您可以添加新类型的电话号码

    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; }
        }