Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add migrations for entity types with constructors #20806

Closed
DurgaPrasadReddyV opened this issue May 1, 2020 · 1 comment
Closed

Comments

@DurgaPrasadReddyV
Copy link

DurgaPrasadReddyV commented May 1, 2020

I have an entity with certain properties. In order to follow DDD principles, the entity properties are initialized via constructor. However when I try run add-migrations command, somehow the migrations are not getting created. If I understand correctly, this feature is added in ef core 2.1 and I am currently using ef core 3.1

Steps to reproduce

Inline are the entities used. Entity can be ignored and replaced with a Guid property in Vehicle.cs and VehicleOwner.cs:

Vehicle.cs

    /// <summary>
    /// Vehicle entity.
    /// </summary>
    public class Vehicle : Entity<Guid>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Vehicle"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="licenseNumber">The license number.</param>
        /// <param name="make">The make.</param>
        /// <param name="model">The model.</param>
        /// <param name="vehicleType">Type of the vehicle.</param>
        /// <param name="vehicleOwner">The vehicle owner.</param>
        public Vehicle(
            Guid id,
            string licenseNumber,
            string make,
            string model,
            VehicleType vehicleType,
            VehicleOwner vehicleOwner)
            : base(id)
        {
            this.LicenseNumber = licenseNumber;
            this.Make = make;
            this.Model = model;
            this.VehicleType = vehicleType;
            this.VehicleOwner = vehicleOwner;
        }

        /// <summary>
        /// Gets the license number.
        /// </summary>
        /// <value>
        /// The license number.
        /// </value>
        public string LicenseNumber { get; private set; }

        /// <summary>
        /// Gets the make of vehicle.
        /// </summary>
        /// <value>
        /// The maker.
        /// </value>
        public string Make { get; private set; }

        /// <summary>
        /// Gets the model of vehicle.
        /// </summary>
        /// <value>
        /// The model.
        /// </value>
        public string Model { get; private set; }

        /// <summary>
        /// Gets the type of vehicle.
        /// </summary>
        /// <value>
        /// The type.
        /// </value>
        public VehicleType VehicleType { get; private set; }

        /// <summary>
        /// Gets the vehicle owner.
        /// </summary>
        /// <value>
        /// The vehicle owner.
        /// </value>
        public VehicleOwner VehicleOwner { get; private set; }

        /// <summary>
        /// Updates the vehicle.
        /// </summary>
        /// <param name="licenseNumber">The license number.</param>
        /// <param name="make">The make.</param>
        /// <param name="model">The model.</param>
        /// <param name="vehicleType">Type of the vehicle.</param>
        /// <param name="vehicleOwner">The vehicle owner.</param>
        public void UpdateVehicle(
            string licenseNumber,
            string make,
            string model,
            VehicleType vehicleType,
            VehicleOwner vehicleOwner)
        {
            this.LicenseNumber = licenseNumber;
            this.Make = make;
            this.Model = model;
            this.VehicleType = vehicleType;
            this.VehicleOwner = vehicleOwner;
        }
    }

VehicleOwner.cs

    /// <summary>
    /// Vehicle owner entity.
    /// </summary>
    /// <seealso cref="Entity{Guid}" />
    public class VehicleOwner : Entity<Guid>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleOwner"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="corporateID">The corporate identifier.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="middleName">Name of the middle.</param>
        public VehicleOwner(
            Guid id,
            string corporateID,
            string firstName,
            string lastName,
            string middleName)
            : base(id)
        {
            this.CorporateID = corporateID;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.MiddleName = middleName;
        }

        /// <summary>
        /// Gets the corporate identifier.
        /// </summary>
        /// <value>
        /// The corporate identifier.
        /// </value>
        public string CorporateID { get; private set; }

        /// <summary>
        /// Gets the first name.
        /// </summary>
        /// <value>
        /// The first name.
        /// </value>
        public string FirstName { get; private set; }

        /// <summary>
        /// Gets the last name.
        /// </summary>
        /// <value>
        /// The last name.
        /// </value>
        public string LastName { get; private set; }

        /// <summary>
        /// Gets the name of the middle.
        /// </summary>
        /// <value>
        /// The name of the middle.
        /// </value>
        public string MiddleName { get; private set; }

        /// <summary>
        /// Updates the vehicle owner.
        /// </summary>
        /// <param name="corporateId">The corporate identifier.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="middleName">Name of the middle.</param>
        public void UpdateVehicleOwner(
            string corporateId,
            string firstName,
            string lastName,
            string middleName)
        {
            this.CorporateID = corporateId;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.MiddleName = middleName;
        }
    }

Exception

System.InvalidOperationException: No suitable constructor found for entity type 'Vehicle'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'vehicleOwner' in 'Vehicle(Guid id, string licenseNumber, string make, string model, VehicleType vehicleType, VehicleOwner vehicleOwner)'.
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.ConstructorBindingConvention.ProcessModelFinalized(IConventionModelBuilder modelBuilder, IConventionContext`1 context)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelFinalized(IConventionModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelFinalized(IConventionModelBuilder modelBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel()
   at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   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.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

No suitable constructor found for entity type 'Vehicle'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'vehicleOwner' in 'Vehicle(Guid id, string licenseNumber, string make, string model, VehicleType vehicleType, VehicleOwner vehicleOwner)'.

Further technical details

EF Core version: 3.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.4.2

@ajcvickers
Copy link
Contributor

Duplicate of #17123 and #12078

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants