2

What I try to reach is an NM relationship between AbstractUser and Plant in use with TPT inheritance.

enter image description here

When I try to configure the relationship,

modelBuilder.Entity<Plant>().HasMany(c => c.RemindedByUsers).WithMany().Map(m =>
{
    m.ToTable("UserHasRemindedPlant");
});

I get the following exeption:

Additional information: Introducing FOREIGN KEY constraint 'FK_dbo.UserHasRemindedPlant_dbo.AbstractUsers_AbstractUser_Id' on table 'UserHasRemindedPlant' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

How do I set the ON DELETE or ON UPDATE action on many to many releationships on tpt abstract entities?

When I use the line above everything works fine but I need this convention for other entities.

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

Here my entity classes:

AbstractUser

public abstract class AbstractUser
{
    protected AbstractUser()
    {
        RemindedPlants = new Collection<Plant>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    ...


    [Required]
    public SchoolYear SchoolYear { get; set; }

    public virtual ICollection<Plant> RemindedPlants { get; private set; }
}

Studend

[Table("Student")]
public class Student : AbstractUser
{
    //some unrelevant props here

}

Teacher

[Table("Teacher")]

public class Teacher : AbstractUser
{
    //some unrelevant props here

}
Nando
  • 813
  • 2
  • 8
  • 18
  • The problem is somewhere else. If it works when removing the `OneToManyCascadeDeleteConvention` the problem can't be the many-to-many relationship. Does for example the `Plant` have a (required) reference to a user (AbstractUser or one of the derived users)? This would indeed cause multiple delete paths from the `AbstractUser` table to the `UserHasRemindedPlant` table and throw the exception. – Slauma Apr 18 '14 at 17:05

2 Answers2

1

You must disable cascading delete with Fluent API .WillCascadeOnDelete(false):

modelBuilder.Entity<Plant>().HasMany(c => c.RemindedByUsers).WithMany().WillCascadeOnDelete(false).Map(m =>{
m.ToTable("UserHasRemindedPlant");});
0

Try this:

modelBuilder.Conventions.Remove();

see: https://stackoverflow.com/a/13705637/491181

Community
  • 1
  • 1
ridermansb
  • 10,779
  • 24
  • 115
  • 226