Error when working with derived classes
See original GitHub issueDescribe the bug My EF Core model has entities that use inheritance. Deteached seems to fail when working with these classes. Whenever it needs to map an object that has children that use inheritance, it tries to instantiate the base class, instead of using one of the derived classes, giving an error if the base class is abstract (or building the incorrect object anyway if it is not).
To Reproduce Define a EF Core model that uses inheritance, like for example:
modelBuilder.Entity<TaskTriggerBase>()
.HasDiscriminator(x => x.TriggerType)
.HasValue<TaskTriggerCron>(TaskTriggerType.CronSchedule)
.HasValue<TaskTriggerReactiveQlik>(TaskTriggerType.AfterQlikTask)
.HasValue<TaskTriggerReactiveEpr>(TaskTriggerType.AfterEpReporterTask);
modelBuilder.Entity<TaskTriggerCron>();
modelBuilder.Entity<TaskTriggerReactiveEpr>();
modelBuilder.Entity<TaskTriggerReactiveQlik>();
and have an entity like this:
public class Entity{
[Composition]
public IEnumerable<TaskTriggerBase> Triggers {get; set;} //this collection can contain instances of any subclass of TaskTriggerBase
//... other properties...
}
then, try to map it with Deteached. You will get an error like this:
System.InvalidOperationException: 'Can't construct TaskTriggerBase. It does not have a parameterless constructor or a concrete type specified.'
Expected behavior Deteached should check the actual instance type of the entities and instantiate the correct subclass instead of the base class.
Additional context Using EF Core 6 on .NET 6, latest version of Deteached available on NuGet.
Issue Analytics
- State:
- Created a year ago
- Comments:12 (9 by maintainers)
Top GitHub Comments
Hi @tarockx, thank you for the feedback! I’ll be able to work on this next friday.
Inheritance works better in version 6.1.0. Also handling abstract types has been improved.