简体   繁体   中英

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:

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. But still I receive the following error when trying to run " Update-Database " inside the console:

Introducing FOREIGN KEY constraint 'FK_dbo.User_dbo.PhoneNumber_FaxId' on table 'User' 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.

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?

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:

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM