Исключение Entity Framework: не найден подходящий конструктор для типа сущности 'CultureInfo' - PullRequest
0 голосов
/ 07 февраля 2019

У меня есть

класс модели

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 **/
}

Репозиторий

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();
    }
}

Ошибка

Исключение Entity Framework: не найден подходящий конструктор для типа сущности 'CultureInfo'

enter image description here

Я пытался найти решение, но не нашел, что здесь может быть не так?: /

полное исключение

{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 голосов
/ 07 февраля 2019

Где-то, кажется, у вас есть свойство в одной из ваших сущностей, например:

public CultureInfo Foo { get; set; }

Поскольку CultureInfo является классом, Entity Framework будет обрабатывать его как сущность и может даже создаватьТаблица базы данных для него, в зависимости от того, как настроена ваша сущность.В любом случае, когда он пытается построить ваш граф объектов, он пытается заполнить это свойство, но не может, потому что CultureInfo не имеет конструктора без параметров.

Длинно и коротко, найдите, где высделал это и удали это.Если вам нужно как-то сохранить информацию о культуре, сохраните что-то, что вы можете использовать, чтобы получить соответствующий экземпляр CultureInfo позже.Вы не можете иметь это на своей сущности, хотя.

...