3

In my Entity Framework 6 code-first project, I have two tables with a many:many relationship.

In Fluent API, I defined the relationship like this:

        modelBuilder.Entity<Profile>()
            .HasMany(e => e.Recipes)
            .WithMany(e => e.Profiles)
            .Map(m => m.ToTable("ProfileRecipe")
            .MapLeftKey("Profiles_ID")
            .MapRightKey(new[] { "Recipes_Name" , "Recipes_Prefix" }));

How is it possible to set the cascade on delete to false in this case?

.WillCascadeOnDelete(false);

is not available.

Edit 1:

Here is the code for both tables:

public partial class Profile
{
    public Profile()
    {
        Recipes = new HashSet<Recipe>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    public virtual ICollection<Recipe> Recipes { get; set; }
}

public partial class Recipe
{
    public Recipe()
    {
        Profiles = new HashSet<Profile>();
    }

    [Key]
    [Column(Order = 0)]
    [StringLength(200)]
    public string Name { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(200)]
    public string Prefix { get; set; }

    public virtual ICollection<Profile> Profiles { get; set; }
}

Edit 2:

Cascade delete is on

you will see it in the migrationscript:

    CreateTable(
    "dbo.ProfileRecipe",
    c => new
            {
            Profiles_ID = c.Int(nullable: false),
            Recipes_Name = c.String(nullable: false, maxLength: 200),
            Recipes_Prefix = c.String(nullable: false, maxLength: 200),
            })
    .PrimaryKey(t => new { t.Profiles_ID, t.Recipes_Name, t.Recipes_Prefix })
    .ForeignKey("dbo.Profiles", t => t.Profiles_ID, cascadeDelete: true)
    .ForeignKey("dbo.Recipes", t => new { t.Recipes_Name, t.Recipes_Prefix }, cascadeDelete: true)
    .Index(t => t.Profiles_ID)
    .Index(t => new { t.Recipes_Name, t.Recipes_Prefix });
meppl
  • 73
  • 1
  • 8
  • This would be easier if you included the code for your table entities. – Ben Jan 08 '19 at 07:09
  • added the code of the tables. Just 2 tables with simple properties and navigation properties to each other – meppl Jan 08 '19 at 07:21
  • 1
    From what I can see cascade delete should be off by default. This was the opposite problem (enabling cascade delete) but it took extra work to enable: https://stackoverflow.com/questions/32655959/entityframework-codefirst-cascade-delete-for-same-table-many-to-many-relationsh – Ben Jan 08 '19 at 07:39
  • cascade delete is on. See my last edit – meppl Jan 08 '19 at 09:09
  • Ok. In that case I'd follow the example in the answer to the link above and specifically define your join table (ProfileRecipe in your edit), so you can define the cascade option between it and the base tables. – Ben Jan 08 '19 at 10:22
  • Shortly, cascade delete cannot be setup separately when using many-to-many with implicit join table. So you can either disable it for all implicit join tables (as shown in the duplicate link) - by default it is enabled, or switch to model with explicit join entity. – Ivan Stoev Jan 08 '19 at 12:08

0 Answers0