I\'m a very strange behavior with EF code first approach and associations. I have two entities:
public class GlobalKpiSectionn
{
public GlobalKpiSection(
To force cascade deletes, you have to use the fluent configurations. You can then remove the [Required]
attribute from the KpiSection property.
Something like this:
public class GlobalKpiSectionn
{
public GlobalKpiSection()
{
this.Regions = new HashSet();
}
public virtual ICollection Regions { get; protected set; }
}
public class Region
{
public int RegionId { get; set; }
public bool IsMain { get; set; }
public int GlobalKpiSectionId { get; set; }
public virtual GlobalKpiSection KpiSection { get; set; }
}
public class RegionConfig : EntityTypeConfiguration
{
HasRequired(x => x.KpiSection)
.WithMany(x => x.Regions)
.HasForeignKey(x => x.GlobalKpiSectionId)
.WillCascadeOnDelete(true);
}
public class YourContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new RegionConfig());
}
}