I have two entities shown below:
public class LiftPlan
{
public int Id { get; set; }
public ICollection<PlanConsiderationEntity> PlanConsiderations { get; set; }
}
public class PlanConsideration
{
public int Id { get; set; }
public int PlanId { get; set; }
public LiftPlan LiftPlan { get; set; }
}
The relationship is configured with -
builder
.HasOne(e => e.LiftPlan)
.WithMany(e => e.PlanConsiderations)
.HasForeignKey(e => e.PlanId)
.OnDelete(DeleteBehavior.NoAction);
It works when I add child entity like -
LiftPlan.PlanConsiderations.Add(planConsideration)
But when I try to remove child entity like
LiftPlan.PlanConsiderations.Remove(planConsideration)
it gives me the error -
The association between entities 'LiftPlanEntity' and 'PlanConsiderationEntity' with the key value '{PlanId: 1}' has been severed but the relationship is either marked as 'Required' or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, then setup the relationship to use cascade deletes.
I tried using -
dbContext.PlanConsiderations.Remove(planConsideration)
and it worked using dbContext.
I am not sure why this is happening. It should work as we are not deleting parent. If we were deleting parent then this error make sense. But for child deletion it should not happen.
Note:
I know by setting relationship to cascade deletes or making foreign key as null will work but I don't want to do that.