I have a principal entity:
public class Predmeti
{
public long PredmetId { get; set; }
//...
public int EntityTypeId { get { return (int)EntityTypesEnum.pravno_Predmeti; } set { } }
public virtual IEnumerable<EntitiesStranke> EntitiesStranke { get; set; }
}and a dependant entity:
public class EntitiesStranke
{
public long EntityStrankaId { get; set; }
public int EntityTypeId { get; set; }
public long EntityId { get; set; }
//...
}I've made EntitiesStranke a generic dependant entity that can have multiple principal entities, EntityTypeId defines which one it is.
The foreign key should be EntityTypeId and EntityId, that is why I've defined Predmeti.EntityTypeId which should be static and should not get filled from the database because that column does not exist and should not exist on that table in the database.
My goal is to have a cascade delete with EF of EntitiesStranke when the Predmeti principal is deleted because I can't create such a relationship in the database due to having PredmetId as the primary key for Predmeti and I don't want to use triggers.
This is the EF Predmeti configuration and relationship:
public class PredmetiEC : IEntityTypeConfiguration<Predmeti>
{
public void Configure(EntityTypeBuilder<Predmeti> builder)
{
builder.ToTable(nameof(Predmeti), "pravno");
builder.HasKey(e => e.PredmetId);
Predmeti predmeti;
EntitiesStranke entitiesStranke;
builder.HasMany(e => e.EntitiesStranke).WithOne()
.HasPrincipalKey(nameof(predmeti.EntityTypeId), nameof(predmeti.PredmetId))
.HasForeignKey(nameof(entitiesStranke.EntityTypeId), nameof(entitiesStranke.EntityId))
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}
}Am I forced to create my own queries for the deletion of dependants or is EF capable of doing this?