简体   繁体   English

实体框架异常:找不到适合实体类型“CultureInfo”的构造函数

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

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'实体框架异常:找不到适合实体类型“CultureInfo”的构造函数

在此处输入图片说明

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}

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.由于CultureInfo是一个类,因此实体框架会将其视为一个实体,甚至可能会为其创建一个数据库表,具体取决于实体的设置方式。 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.在任何一种情况下,当它尝试构建您的对象图时,它都会尝试填充此属性,但不能,因为CultureInfo没有无参数构造函数。

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.如果您需要以某种方式保留文化信息,请保留一些您可以稍后用于获取适当CultureInfo实例的内容。 You can't have it on your entity itself though.但是,您不能在实体本身上使用它。

I had this same problem as well.我也有同样的问题。 In my case I had a near empty data access layer project.就我而言,我有一个近乎空的数据访问层项目。 The problem came and went as I created a navigation link to an entity type that is not available in the DbContext as a DbSet.当我创建一个指向实体类型的导航链接时,问题来了又去,该实体类型在 DbContext 中作为 DbSet 不可用。

for example the code underneath gave the same problem OP had:例如,下面的代码给出了与 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; }
}

While this code didn't:虽然这段代码没有:

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

The problem could be solved in two ways:问题可以通过两种方式解决:

  • out commenting the navigation link出来评论导航链接
  • adding property DbSet<Pipeline> to the DbContext.将属性DbSet<Pipeline>添加到 DbContext。

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).对于遇到此问题并且没有任何意见或解决方案解决您的问题的任何人,请确保您的表实体没有继承自 IDisposable 的某些内容(或可能只是不继承自 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".就我而言,我正在阅读的一些教程(公平地说,可能对我自己的好处来说太快了一点)继承了“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.我当时处于“非常快速地尝试 200 件事模式”并且忘记了我是用我的许多表实体之一继承它的。

The error you get is very non-specific & vague, so hopefully this helps someone out there!您得到的错误非常不具体和模糊,所以希望这可以帮助那里的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到适合实体类型“XName”的构造函数 - No suitable constructor found for entity type 'XName' 找不到适合实体类型“CoordinateSystem”的构造函数 - No suitable constructor found for entity type 'CoordinateSystem' 找不到适合实体类型 MyImage 的构造函数 - No suitable constructor found for entity type MyImage 找不到适合实体类型字符串的构造函数 - No suitable constructor found for entity type string 没有为实体类型“Uri”找到合适的构造函数 - No suitable constructor found for entity type 'Uri' 没有为实体类型 HierarchyId 找到合适的构造函数 - No suitable constructor found for entity type HierarchyId 找不到实体类型“MyEntity”的合适构造函数 - No suitable constructor was found for entity type "MyEntity" System.InvalidOperationException:找不到适合实体类型“BoundingBox”的构造函数 - System.InvalidOperationException: No suitable constructor found for entity type 'BoundingBox' DBContext 失败没有为实体类型“文件夹名称空间”找到合适的构造函数 - DBContext fail No suitable constructor was found for entity type 'FolderNamespace' .Net Razor 页面错误:没有为实体类型“端点”找到合适的构造函数 - .Net Razor Pages Error: No suitable constructor was found for entity type 'Endpoint'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM