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.

Allow more flexible conditional mapping for TPH and similar

See original GitHub issue

I have the following model

public class BaseType
    {
        public string Code {get;set;}
        public string Type {get;set;}
}
public class ChildType:BaseType
    {
        public ChildType()=> Type = "ExplicitChild"
        public string AdditionalData {get;set;}
}

I am able to create a BaseType with a type value as “Implicit Child” and save it to the database, but when retrieving the information I can not get a reference to the “Implicit Child.”

db.Set<BaseType>().ToList();

This is caused by the discriminator column being present in the where clause at all times.

Where [e].[Type] IN (N'ExplicitChild', N'BaseType')

I would expect one of the following

  • Prevent saving of arbitrary discriminator.
  • Don’t check the discriminator column at all when using the base set without any constraints. <== preferred for my case

Further technical details

EF Core version: 2.0.0 Database Provider: SqlLite, MSSql Operating system: Windows 10 IDE:Visual Studio 2017

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:21
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
Tarig0commented, Dec 19, 2017

on your point on what would I expect when creating a base type, I would expect the discriminator to have to be explicitly defined, before attaching the entity.

Another option is to add a fluent function to the hasdiscriminator path, something like “HasDefaultDiscriminator” or have hasvalue<T> have the overload that takes an ienumerable<t> require a T param to be passed for the default

typeEnum Type {get;set}

modelBuilder.Entity<BaseClass>().HasDiscriminator(e=>e.Type)
   .HasValue<ExplicitChild>(typeEnum.ExplicitChild)
   .HasValue<BaseClass>(typeEnum.Child1,typeEnum.Child2)
   .HasDefaultDiscriminator<BaseClass>(typeEnum.Child1);

modelBuilder.Entity<BaseClass>().HasDiscriminator(e=>e.Type)
   .HasValue<ExplicitChild>(typeEnum.ExplicitChild)
   .HasValue<BaseClass>(default:typeEnum.Child1, typeEnum.Child1,typeEnum.Child2)
2reactions
gojanpaolocommented, Nov 24, 2019

Are there any workarounds to assign multiple discriminator values for a single derived type?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Mapping in Entity Framework - OR operation ...
We have an entity like Vehicle and three derived entities such as Car , Motorbike and Bicycle . This inheritance hierarchy is implemented...
Read more >
What's New in EF Core 5.0
By default, EF Core maps an inheritance hierarchy of .NET types to a single database table. This is known as table-per-hierarchy (TPH) mapping....
Read more >
4 TPH Fundamentals
Representative aliphatic and aromatic hydrocarbons. Another key feature of petroleum hydrocarbons is that they typically have a large number of isomers. Isomers ...
Read more >
Incremental Mapping Compilation in an Object-to- ...
We start by describing an SMO that adds an entity type using the most common strategies for mapping entity types to tables, namely,....
Read more >
Inheritance - Devart Entity Developer Documentation
The TPH inheritance depends on a conditional mapping which is defined by a condition such as a discriminator database field. This condition is...
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