How to fix No suitable constructor found

c# entity-framework-core enums mapping

Question

I have to use Enum Safe Pattern to persist in the database, just the code of an enum. When I try to run the migration, I get the error:

No suitable constructor found for entity type 'Regiao'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'codigo', 'nome' in 'Regiao(int codigo, string nome)'

I already reviewed the code, changed the constructor from protected to public, but it had no effect.

This is base class.

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

This is the class that inherits from 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) { }
    }

This is the class where I use the 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)
        {

        }
    }

and finally the code snippet where I do the mapping

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

I expected the Codigo field to be mapped to the database, and on reading the Code field to be mapped back to the Regiao type. But I get the message:

System.InvalidOperationException HResult=0x80131509 Message=No suitable constructor found for entity type 'Regiao'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'codigo', 'nome' in 'Regiao(int codigo, string nome)'. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConstructorBindingConvention.Apply(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.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.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() at Bigai.CepApi.Data.Initializers.CepApiInitializer.Initialize(CepApiContext context) in D:\Projects\Dev\Bigai\CepApi\src\Bigai.CepApi.Data\Initializers\CepApiInitializer.cs:line 17 at Bigai.CepApi.Services.Api.Configurations.ApiConfiguration.UseApiConfiguration(IApplicationBuilder app, CepApiContext context, IApiVersionDescriptionProvider provider) in D:\Projects\Dev\Bigai\CepApi\src\Bigai.CepApi.Services.Api\Configurations\ApiConfiguration.cs:line 48 at Bigai.CepApi.Services.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, CepApiContext context, IApiVersionDescriptionProvider provider) in D:\Projects\Dev\Bigai\CepApi\src\Bigai.CepApi.Services.Api\Startup.cs:line 74

How to fix this, please?

1
1
7/31/2019 12:12:26 AM

Accepted Answer

Thank you @Christopher and @GPW.

I solved the problem. In fact, the solution was right in front of me, all I had to do was create a protected builder needed for our friend Entity Framework.

In my base class I put

protected EnumBase()

And in the derived class:

protected Regiao() : base() { }

These two changes solved the problem.

0
8/1/2019 6:44:05 PM


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