Apparently Cascade Deleting with Entity Framework is very confusing. I found lot's of questions but I have not found a solution for my problem. If I manually set the "Delete Rule" to cascade it all works. But via Fluent API I haven't found a way to set this property in the database.
In my case I have a base class (with it's own table in the database)
public abstract class PropertyBase
{
public int PropertyID { get; set; }
public string Name { get; set; }
public virtual AspectBase AspectBase { get; set; }
}
And I have derived classes which are stored Table Per Type.
public class TextProperty : PropertyBase
{
public string Value { get; set; }
}
public class IntProperty : PropertyBase
{
public int Value { get; set; }
}
In my database context I have the following:
modelBuilder.Entity<PropertyBase>()
.ToTable("Properties")
.HasKey(p => p.PropertyID);
modelBuilder.Entity<IntProperty>()
.ToTable("IntProperties");
modelBuilder.Entity<TextProperty>()
.ToTable("TextProperties");
Below is a snapshot of the database table for "IntProperties".
I need to set the Delete rule of the selected foreignkey in the image above. If I do this manually in the database, then it all works fine. I really have no idea on how to achieve this. Please help.
I know it has to be something with WillCascadeOnDelete, but to be able to do this with Fluent API I probably need some navigation properties?
This is not a duplicate question: because in my case it's a derived class of the abstract base class. Some have mentioned that it is not possible with an existing database, but luckily I don't have an existing database. I am trying to accomplish this Code First and with Fluent API. Any help is very welcome!