I have 2 tables called Domain and Domain Theme. it has one to one relationship. Here the 2 model classes.
public class Domain
{
public int OrganizationDomainId { get; set; }
[MaxLength(200), Required]
public string Domain { get; set; }
public bool Active { get; set; }
public int? ThemeId { get; set; }
[ForeignKey("ThemeId")]
public virtual OrganizationDomainTheme OrganizationDomainTheme {get;set;}
}
public class OrganizationDomainTheme
{
public int OrganizationDomainThemeId { get; set; }
public string PrimaryColor{get;set;}
public virtual OrganizationDomain OrganizationDomain { get; set; }
}
I have implemented a delete behavior by using model builder for my 2 tables.
modelBuilder.Entity<OrganizationDomainTheme>()
.HasOne(odt => odt.OrganizationDomain)
.WithOne(od => od.OrganizationDomainTheme)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<OrganizationDomain>()
.HasOne(od => od.OrganizationDomainTheme)
.WithOne(odt=>odt.OrganizationDomain)
.OnDelete(DeleteBehavior.Cascade);
Senario.
When i delete a Theme it sets the Domain Null as i expected from number 1.
But When i delete a Domain it will not delete the Theme. Theme is still on the table.
Am i configure it wrong ? Please Help.
I want to delete the theme that belong to the domain when i delete a domain.
In one-to-one relationship, the entity with foreignkey is "dependent entity" (Domian). The other is " principal entity" (Theme).
All the Deletebehaviour only take effect on dependent entity when you delete principal eneity. https://entityframeworkcore.com/saving-data-cascade-delete
AFAIK, you have to delete that principal entity manually after delete the denpendent entity.