question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when working with derived classes

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
leonardoporrocommented, Apr 12, 2022

Hi @tarockx, thank you for the feedback! I’ll be able to work on this next friday.

0reactions
leonardoporrocommented, May 31, 2022

Inheritance works better in version 6.1.0. Also handling abstract types has been improved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error while accessing derived class resources by base ...
I have base class called Base and a derived class of Base called Derived, A Base Class pointer can point to a derived...
Read more >
Derived Classes and Inheritance
The base class may have private member variables, which cannot be accessed from the derived class, so the derived class does not initialize...
Read more >
Catching Base and Derived Classes as Exceptions in C++ ...
Let's discuss what is Exception Handling and how we catch base and derived classes as an exception in C++:. If both base and...
Read more >
Inheritance — What your mother never told you, C++ FAQ
Why doesn't overloading work for derived classes? ... use this idiom is to make life easier and less error-prone for the developers of...
Read more >
Derived classes
Any class type (whether declared with class-key class or struct ) may be declared as derived from one or more base classes which,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found