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.

Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping'

See original GitHub issue
public abstract class MultiLangEntity
{        
    [StringLength(3)]
    public string LanguageCode { get; set; }
    
    [ForeignKey("LanguageCode")]        
    public virtual Language Language { get; set; }

    [StringLength(50)]
    public virtual string Content { get; set; }
}

public class CountryDescription : MultiLangEntity
{
    [ForeignKey("CountryCode")]
    public virtual Country Country { get; set; }

    [StringLength(3)]
    public string CountryCode { get; set; }        
}

Initially, I use EF Core 2.0. The above code works. Then I realized lazy load only supported by EF Core 2.1. When I swift to “EF core 2.1.0-preview2-final”, I encounter following exceptions.

System.InvalidCastException: Unable to cast object of type ‘ConcreteTypeMapping’ to type ‘Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping’. at Microsoft.EntityFrameworkCore.Metadata.RelationalPropertyAnnotations.get_ColumnType() at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerTypeMapper.GetColumnType(IProperty property) at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapper.FindMapping(IProperty property) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationalTypeMappingConvention.Apply(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.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.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:27 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
ajcvickerscommented, Apr 17, 2018

@mgolois When using ASP.NET Core, you need to update to the latest .NET Core SDK, as described in the ASP.NET Core 2.1 preview2 announcement. Currently you still have .NET Core 2.0 and ASP.NET Core 2.0 referenced, with only some EF packages updated to the 2.1 versions. This configuration will not work. Updating all the EF packages may work, but is not recommended–the recommended path is to install the new SDK.

4reactions
ajcvickerscommented, Jun 1, 2018

For anyone reading this thread: if you are seeing these errors, then it likely means you have package version mismatch, as has been described in previous posts. If you are still seeing this after updating either SDK (for ASP.NET Core apps) or all EF package versions (for other apps) then please post a project/solution that exhibits the behavior you are seeing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - "Unable to cast object of type 'ConcreteTypeMapping ...
As per https://github.com/aspnet/EntityFrameworkCore/issues/11704 you have a version mismatch. Some of your Entity Framework versions are ...
Read more >
Unable to cast object of type 'ConcreteTypeMapping ...
Unable to cast object of type 'ConcreteTypeMapping' to type 'Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping'
Read more >
RelationalTypeMapping Class
This type is typically used by database providers (and other extensions). ... Storage. Assembly: Microsoft.EntityFrameworkCore.Relational.dll.
Read more >
System.InvalidCastException: Unable to cast object of type ...
InvalidCastException: Unable to cast object of type 'System. ... How can I convert the type of Size from type bigint to type int...
Read more >
Unable to cast object of type 'Microsoft.Extensions ...
Coding example for the question Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope'
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