Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Configure Nested Discriminators #15870

Closed
todd-skelton opened this issue May 30, 2019 · 4 comments
Closed

Cannot Configure Nested Discriminators #15870

todd-skelton opened this issue May 30, 2019 · 4 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-enhancement

Comments

@todd-skelton
Copy link

I'm trying to configure entities that have discriminators at multiple levels. I can't seem to figure out how to do so.

Exception

Unhandled Exception: System.InvalidOperationException: A discriminator property cannot be set for the entity type 'NestedEntity' because it is not the root of an inheritance hierarchy.
   at Microsoft.EntityFrameworkCore.Metadata.RelationalEntityTypeAnnotations.SetDiscriminatorProperty(IProperty value, Type oldDiscriminatorType)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalEntityTypeBuilderAnnotations.DiscriminatorBuilder(Func`2 createProperty, Type propertyType)
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.HasDiscriminator[TDiscriminator](EntityTypeBuilder entityTypeBuilder, String name)
   at EfCoreNestedDiscriminators.MyContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\admin\Source\Local\EfCoreNestedDiscriminators\Program.cs:line 57
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at EfCoreNestedDiscriminators.Program.Main(String[] args) in C:\Users\admin\Source\Local\EfCoreNestedDiscriminators\Program.cs:line 13

Steps to reproduce

    public abstract class BaseEntity
    {
        public int Id { get; set; }
        public BaseType Type { get; set; }
    }

    public enum BaseType
    {
        NestedEntity
    }

    public abstract class NestedEntity : BaseEntity
    {
        public NestedType NestedType { get; set; }
    }

    public class ConcreteEntity : NestedEntity
    {

    }

    public enum NestedType
    {
        ConcreteEntity
    }

    public class MyContext : DbContext
    {
        public DbSet<BaseEntity> Entities { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Server=(localdb)\mssqllocaldb;Database=EfCoreNestedDiscriminators;Integrated Security=True");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BaseEntity>().HasDiscriminator<BaseType>("Type").HasValue<NestedEntity>(BaseType.NestedEntity);

            modelBuilder.Entity<NestedEntity>().HasDiscriminator<NestedType>("NestedType").HasValue<ConcreteEntity>(NestedType.ConcreteEntity);
        }
    }

Further technical details

EF Core version: 2.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Visual Studio 2019

@ajcvickers
Copy link
Member

@xKloc This is not a mapping that is supported by EF Core. Also, the value of the feature when compared against the implementation cost makes it something that we have plans to implement any time soon, and possibly never, depending on the level of additional feedback we get indicating the importance of this kind of mapping.

@todd-skelton
Copy link
Author

No problem, I ended up flattening my discriminators.

@AndriySvyryd
Copy link
Member

Related to #10140

@ajcvickers
Copy link
Member

Closing as this is no longer something we intend to implement.

@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed propose-close labels Nov 16, 2019
@ajcvickers ajcvickers removed this from the Backlog milestone Nov 16, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants