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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 27 (8 by maintainers)

Most upvoted comments

@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.

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.

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.

@ajcvickers Oh no! To make lazy loading using proxy to work, do I also need to use .net core 2.1 sdk? I follow tutorials and cannot work, very sad!

just update all package Microsoft.EntityFrameworkCore.XXXX and Microsoft.AspNetCore.XXXX to prerelease versions (2.1.0-preview2-final

image

@ajcvickers Greetings, I made a mistake and did not update certain packages to net core 2.1, then it worked correctly. Thanks for your help.