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