实体框架例外:找不到适用于实体类型'CultureInfo'

时间:2019-02-07 14:40:22

标签: c# entity-framework-core asp.net-core-2.1

我有这个

模型类

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

错误

  

实体框架异常:找不到适合实体的构造函数   输入“ 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}

3 个答案:

答案 0 :(得分:3)

我也有同样的问题。就我而言,我有一个几乎空的数据访问层项目。当我创建到实体类型的导航链接时,问题就来了又去了。在DbContext中,该实体类型不能作为DbSet使用。

例如,下面的代码也给OP带来了同样的问题:

    public class Organization
    {
        [Key]
        public Guid OrganizationId { get; set; }
        public string Name { get; set; }

        // Navigation properties
        public virtual ICollection<Pipeline> Pipelines { get; set; }
    }

    public class MyContext : DbContext
    {
        public MyContext()
        {
        }

        public MyContext(DbContextOptions<MyContext> dbContextOptions) : base(dbContextOptions)
        {

        }

        public DbSet<Organization> Organizations { get; set; }
}

该代码没有:

    public class Organization
    {
        [Key]
        public Guid OrganizationId { get; set; }
        public string Name { get; set; }

        // Navigation properties
 // COMMENTED OUT       public virtual ICollection<Pipeline> Pipelines { get; set; }
    }

    public class MyContext : DbContext
    {
        public MyContext()
        {
        }

        public MyContext(DbContextOptions<MyContext> dbContextOptions) : base(dbContextOptions)
        {

        }

        public DbSet<Organization> Organizations { get; set; }
}

可以通过两种方式解决该问题:

  • 不评论导航链接
  • 将属性DbSet<Pipeline>添加到DbContext。

答案 1 :(得分:1)

在某处,似乎您对其中一个实体拥有财产,例如:

public CultureInfo Foo { get; set; }

由于CultureInfo是一个类,因此Entity Framework会将其视为一个实体,甚至可能会为其创建数据库表,具体取决于您的实体设置方式。无论哪种情况,当它试图构建对象图时,它都试图填充该属性,但是不能这样做,因为CultureInfo没有无参数的构造函数。

长短不一,找到您在哪里做的并将其删除。如果您需要以某种方式保留区域性信息,则可以保留一些内容,以后可以使用它们来获取适当的CultureInfo实例。但是,您不能在实体本身上使用它。

答案 2 :(得分:1)

对于遇到此问题且没有任何评论或解决方案可以解决您的问题的任何人,请确保您的表实体没有继承自IDisposable继承的东西(或者可能不是继承自System.Data.DataTable)。 / p>

就我而言,我正在阅读的一些教程(出于公平考虑,可能有点太快了)是继承“ System.Data.DataTable”。我当时处于“快速尝试200种事物”模式,却忘记了我是用许多表实体之一继承它的。

您收到的错误非常明确且含糊不清,因此希望可以帮助某个人!