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

Migrations: Exception thrown with model containing TPH in certain cases: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. #8423

Closed
stitch84 opened this issue May 9, 2017 · 9 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@stitch84
Copy link

stitch84 commented May 9, 2017

Describe what is not working as expected.

When I try to add a migration via dotnet ef add migrations I get an exception:

I also have a many-to-many relationship in my model which is configured in
OnModelCreating

I figured out that the migration gets created when I remove the code for the many-to-many relationship in OnModelCreating and when I remove the properties for the Join Table class.

My Test project is attached.

Exception message:
The entity type 'SingleExpense' should derive from 'SingleMoneyItem' to reflect the hierarchy of the corresponding CLR types.

Stack trace:
System.InvalidOperationException: The entity type 'SingleExpense' should derive from 'SingleMoneyItem' to reflect the hierarchy of the co
rresponding CLR types.
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.EnsureClrInheritance(IModel model, IEntityType entityType, HashSet`1 validEnt
ityTypes)
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.EnsureClrInheritance(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder,
 IModelValidator validator)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServicePr
ovider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite,
ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCall
Site, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite,
ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type servic
eType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<RemoveMigrationImpl>d__16.MoveNext()
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Steps to reproduce

dotnet ef migrations add Initial

Further technical details

EF Core version: 1.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio Code

WebAPIApplication.zip

@stitch84 stitch84 changed the title Many-to-many produces: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. Migrations with Model containing many-to-many produces: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. May 9, 2017
@ajcvickers
Copy link
Contributor

@stitch84 Can you post a project or complete code listing that reproduces what you are seeing? Specifically, what your entity types look like, including inheritance, and the code you have in OnModelCreating?

@stitch84
Copy link
Author

stitch84 commented May 9, 2017

@ajcvickers Please see my first comment. At the end there is a link to my WebAPIApplication project.

@smitpatel
Copy link
Contributor

I tested the model in the sample project provided in OP. It throws exception as above in 1.1.1 packages. But it works correctly in preview1 bits.
Probable cause of exception - While adding entity type SingleExpense we discover the base types, but for some reason DerivedTypeDiscoveryConvention did not run correctly. Therefore we threw exception. Since we use batching in conventions running, it is running correctly and creating valid model so it works.

Sidenote: You are mixing 1.0.x & 1.1.x packages of ASP.NET core in same project. You should align them.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 9, 2017
@stitch84
Copy link
Author

stitch84 commented May 9, 2017

@smitpatel Sorry for my stupid question, but I didn't get your point.
Will it work in a future release of EF Core?

@smitpatel
Copy link
Contributor

@stitch84 - Yes. The next public release of EF Core with version 2.0.0-preview1 has fix for this issue.

@stitch84
Copy link
Author

stitch84 commented May 9, 2017

@smitpatel Thank you!!!

@stitch84 stitch84 closed this as completed May 9, 2017
@smitpatel smitpatel added this to the 2.0.0-preview1 milestone May 9, 2017
@smitpatel
Copy link
Contributor

@divega - to update title.

@divega divega added the type-bug label May 9, 2017
@divega divega changed the title Migrations with Model containing many-to-many produces: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. Migrations: Exception thrown with Model containing many-to-many: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. May 9, 2017
@divega divega changed the title Migrations: Exception thrown with Model containing many-to-many: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. Migrations: Exception thrown with model containing many-to-many: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. May 9, 2017
@smitpatel smitpatel changed the title Migrations: Exception thrown with model containing many-to-many: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. Migrations: Exception thrown with model containing TPH in certain cases: The entity type should derive from to reflect the hierarchy of the corresponding CLR types. May 9, 2017
@smitpatel
Copy link
Contributor

I removed many-to-many part, since it was not specific to many-to-many table. In this case, it just provided a way to discover types in certain order.

@divega
Copy link
Contributor

divega commented May 9, 2017

@smitpatel thanks!

@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants