Entity Framework exception: No suitable constructor found for entity type 'CultureInfo'

asp.net-core-2.1 c# entity-framework-core

Question

I have this:

model class

public class BusinessSelectList
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsBusinessGroup { get; set; }
    [NotMapped]
    public bool IsSelected { get; set; } = false;
}

DbContext

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }

    public virtual DbQuery<BusinessSelectList> BusinessSelectList { get; set; 

    /** other virtual DbQuery properties **/
}

Repository

public class BusinessRepository :IBusinessRepository
{
    private readonly IHttpContextAccessor _httpAccessor;
    private readonly AppDbContext _context;

    public BusinessRepository(AppDbContext context, IHttpContextAccessor httpAccessor)
    {
        _context = context;
        _httpAccessor = httpAccessor;
    }

    public IEnumerable<BusinessSelectList> GetAsSelectList()
    {
        var query = @"exec webApp.usp_Business_GetAsSelectList @user";
        var p1 = new SqlParameter("user", _httpAccessor.HttpContext.User.Identity.Name);
        return _context.BusinessSelectList.FromSql(query, p1).ToList();
    }
}

Error

Entity Framework exception: No suitable constructor found for entity type 'CultureInfo'

enter image description here

I tried to search for a solution, but I did not find any. What can be wrong here? :/

The full exception is:

{System.InvalidOperationException: No suitable constructor found for entity type 'CultureInfo'. The following parameters could not be bound to properties of the entity: 'name', 'name', 'useUserOverride', 'cultureData', 'isReadOnly', 'culture', 'culture', 'useUserOverride', 'cultureName', 'textAndCompareCultureName'.
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConstructorBindingConvention.Apply(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__1()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   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.InternalDbQuery`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbQuery`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbQuery`1.System.Linq.IQueryable.get_Provider()
   at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSql[TEntity](IQueryable`1 source, RawSqlString sql, Object[] parameters)
   at MBPT.CoreWeb.Repository.BusinessRepository.GetAsSelectList() in C:\Users\czurbanlu\Source\Workspaces\Workspace\MBPT\MBPT.CoreWeb\MBPT.CoreWeb\Repository\BusinessRepository.cs:line 32}
1
0
1/16/2020 7:03:04 PM

Accepted Answer

Somewhere, it seems you have a property on one of your entities like:

public CultureInfo Foo { get; set; }

Since CultureInfo is a class, Entity Framework will treat it as an entity, and may even be creating a database table for it, depending on how your entity is set up. In either case, when it attempts to build your object graph, it's trying to fill this property in, but cannot because CultureInfo doesn't have a parameterless constructor.

Long and short, find where you've done this and remove it. If you need to somehow persist culture information, persist something you can use to get an appropriate CultureInfo instance later. You can't have it on your entity itself though.

4
2/7/2019 2:51:39 PM

Popular Answer

For anyone who runs into this issue and none of thecomments or solutions are resolving your issue, make sure your table entities are not inheriting from something that inherits from IDisposable (or potentially just not inheriting from System.Data.DataTable).

In my case, some tutorial I was reading (probably a little too quickly for my own good, to be fair) was inheriting "System.Data.DataTable". I was in "Try 200 things really quickly mode" and had forgotten I was inheriting it with one of my many table entities.

The error you get is very non-specific & vague, so hopefully this helps someone out there!



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow