1

I have the following fluent mapping:

 modelBuilder.Entity<User>()
                .HasMany<Device>(user => user.Devices)
                .WithMany(sv => sv.Users)
                .Map(userDevice =>
                {
                    userDevice.MapLeftKey("UserId");
                    userDevice.MapRightKey("DeviceId");
                    userDevice.ToTable("UserDevice"));
                });

Inside User:

public virtual ICollection<Device> Devices { get; set; }

Inside Device:

public virtual ICollection<User> Users { get; set; }

Now I receive the following error:

{"Introducing FOREIGN KEY constraint 'FK_dbo.UserDevice_dbo.Device_DeviceId' on table 'UserDevice' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."}

I would love to set "WillCascadeOnDelete(false)" but this option is not available on "WithMany()"

I also found the following solution but I do not want to disable cascade delete completely: Entity Framework Many to Many Cascade Delete Issue

Community
  • 1
  • 1
Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • I think your constraint is slightly off. Do you get the same error if you use `HasMany()` instead of `HasMany()`? – Claies May 18 '15 at 13:40
  • the problem is the `Map`. WillCascadeOnDelete should be ok after the WithMany. Do you really need to set the table and keys for the link table ? – tschmit007 May 18 '15 at 14:19
  • @OleAlbers Could you post the code for the models? I suspect there is another conflicting relationship. – user2697817 May 18 '15 at 14:32

0 Answers0