如何修复找不到合适的构造函数

时间:2019-07-30 20:47:43

标签: c# enums entity-framework-core mapping

我必须使用枚举安全模式来持久存储在数据库中,而只是枚举的代码。当我尝试运行迁移时,出现错误:

找不到适用于实体类型“ Regiao”的合适构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定“ Regiao(int codigo,string nome)”中的“ codigo”,“ nome”

我已经检查过代码,将构造方法从受保护的更改为公共的,但是无效。

这是基类。

    public abstract class EnumBase<TEnum, TKey> :
        IEquatable<EnumBase<TEnum, TKey>>,
        IComparable<EnumBase<TEnum, TKey>>
        where TEnum : EnumBase<TEnum, TKey>
        where TKey : IEquatable<TKey>, IComparable<TKey>
    {
        private readonly TKey _codigo;

        private readonly string _nome;

        private static readonly List<TEnum> _listaDeEnums = new List<TEnum>();

        private static bool _invoked;

        public TKey Codigo => _codigo;

        public string Nome => _nome;

        public static IReadOnlyCollection<TEnum> ListaDeEnums
        {
            get
            {
                if (!_invoked)
                {
                    _invoked = true;
                    typeof(TEnum).GetProperties(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(p => p.PropertyType == typeof(TEnum))?.GetValue(null, null);
                }

                return _listaDeEnums;
            }
        }

        protected EnumBase(TKey codigo, string nome)
        {
            _nome = nome;
            _codigo = codigo;

            TEnum item = this as TEnum;
            _listaDeEnums.Add(item);
        }

        public static TEnum ObterPorNome(string nome)
        {
            return ListaDeEnums.SingleOrDefault(item => string.Equals(item.Nome, nome, StringComparison.OrdinalIgnoreCase));
        }

        public static TEnum ObterPorCodigo(TKey codigo)
        {
            // Can't use == to compare generics unless we constrain TValue to "class", which we don't want because then we couldn't use int.
            return ListaDeEnums.SingleOrDefault(item => EqualityComparer<TKey>.Default.Equals(item.Codigo, codigo));
        }

        public override string ToString()
        {
            return _nome;
        }

        public virtual bool Equals(EnumBase<TEnum, TKey> other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            if (other.GetType() != GetType())
            {
                return false;
            }

            return _codigo.Equals(other._codigo);
        }

        public int CompareTo(EnumBase<TEnum, TKey> other)
        {
            return _codigo.CompareTo(other._codigo);
        }
    }

这是从EnumBase继承的类

    public class Regiao : EnumBase<Regiao, int>
    {
        public static Regiao Indefinida { get; } = new Regiao(0, "Indefinida");
        public static Regiao CentroOeste { get; } = new Regiao(1, "Centro-Oeste");
        public static Regiao Nordeste { get; } = new Regiao(2, "Nordeste");
        public static Regiao Norte { get; } = new Regiao(3, "Norte");
        public static Regiao Sudeste { get; } = new Regiao(4, "Sudeste");
        public static Regiao Sul { get; } = new Regiao(5, "Sul");

        public Regiao(int codigo, string nome) : base(codigo, nome) { }
    }

这是我使用Enum Regiao的课程

    public class Estado
    {
        public int ChaveEstadoDne { get; private set; }

        public string SiglaPais2Pos { get; private set; }

        public string Uf { get; private set; }

        public string CodigoIbgeEstado { get; private set; }

        public string NomeOficialEstado { get; private set; }

        public string NomeAbreviadoEstado { get; private set; }

        public Regiao Regiao { get; private set; }

        public Guid PaisId { get; private set; }

        protected Estado()
        {
        }

        private Estado(Guid id, EntityStatus status, DateTime? dataCadastro, TipoProcesso rotina, int chaveEstadoDne, string siglaPais2Pos, string uf, string codigoIbge, string nomeOficial, string nomeAbreviado, Guid paisId) : base(id, chaveEstadoDne.ToString(), status, dataCadastro, rotina)
        {

        }
    }

最后是我进行映射的代码段

            builder.Property(estado => estado.Regiao)
                .HasColumnName("Regiao")
                .HasConversion(
                    estado => estado.Codigo,
                    estado => Regiao.ObterPorCodigo(estado));

我希望将Codigo字段映射到数据库,并在读取Code字段时将其映射回Regiao类型。但是我得到消息:

System.InvalidOperationException   HResult = 0x80131509   Message =找不到适用于实体类型“ Regiao”的合适构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定“ Regiao(int codigo,string nome)”中的“ codigo”,“ nome”。   源= Microsoft.EntityFrameworkCore   堆栈跟踪:    在Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConstructorBindingConvention.Apply(InternalModelBuilder modelBuilder)    在Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)    在Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()    在System.Lazy 1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy 1.ExecutionAndPublication(LazyHelper执行AndPublication,布尔值useDefaultConstructor)处    在System.Lazy 1.CreateValue() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite,TArgument参数)处    在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围)    在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参数)处    在Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围)    在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.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure 1访问器处)    在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()    在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()    在Bigai.CepApi.Data.Initializers.CepApiInitializer.Initialize(CepApiContext上下文)中的D:\ Projects \ Dev \ Bigai \ CepApi \ src \ Bigai.CepApi.Data \ Initializers \ CepApiInitializer.cs:第17行    在Bigai.CepApi.Services.Api.Configurations.ApiConfiguration.UseApiConfiguration(IApplicationBuilder应用,CepApiContext上下文,IApiVersionDescriptionProvider提供程序)位于D:\ Projects \ Dev \ Bigai \ CepApi \ src \ Bigai.CepApi.Services.Api \ Configurations \ ApiConfiguration中。 cs:第48行    在Bigai.CepApi.Services.Api.Startup.Configure(IApplicationBuilder应用,IHostingEnvironment env,CepApiContext上下文,IApiVersionDescriptionProvider提供程序)中位于D:\ Projects \ Dev \ Bigai \ CepApi \ src \ Bigai.CepApi.Services.Api \ Startup.cs :第74行

请如何解决此问题?

1 个答案:

答案 0 :(得分:0)

谢谢@Christopher和@GPW。

我解决了这个问题。实际上,该解决方案就在我眼前,我所要做的就是为我们的朋友实体框架创建一个受保护的生成器。

我在基类中放了

protected EnumBase()

在派生类中:

protected Regiao() : base() { }

这两个更改解决了问题。