Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

ValueObject requires PrimaryKey #3466


User avatar
0
rafalpiotrowski created

I have ValueObject that later is a prop of an Entity VO is Address below

public class Address : ValueObject<Address>
    {
        public int CountryId { get; private set; } //A reference to a Country entity.
        public string City { get; private set; }

        public string Street { get; private set; }

        public string PostalCode { get; private set; }

        public int Number { get; private set; }

        public Address(int countryId, string city, string street, int number, string postalCode)
        {
            CountryId = countryId;
            City = city;
            Street = street;
            Number = number;
            PostalCode = postalCode;
        }
    }

When doing Add-Migration "whatever" I get

System.InvalidOperationException: The entity type 'Address' requires a primary key to be defined.
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
   at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.&lt;&gt;c__DisplayClass16_0.&lt;RealizeService&gt;b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.&lt;&gt;c__DisplayClass16_0.&lt;RealizeService&gt;b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   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.OperationBase.&lt;&gt;c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'Address' requires a primary key to be defined.
PM>

In the ABP documentation I do not see the PK definition!

<a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/v2.0.1/Value-Objects">https://aspnetboilerplate.com/Pages/Doc ... ue-Objects</a>


7 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    Add**[ComplexType]** attribute to Address class.

    [ComplexType]
        public class Address : IHasCreationTime
        {
            public string Country { get; set; }
    
            public string City { get; set; }
    
            public string FullAddress { get; set; }
     
            //other fields...
        }
    
  • User Avatar
    0
    rafalpiotrowski created

    My Address class: [ComplexType] public class Address : ValueObject<Address> { public Country Country { get; private set; } //A reference to a Country entity. public string City { get; private set; }

        public string Street { get; private set; }
    
        public string PostalCode { get; private set; }
    
        public int Number { get; private set; }
    
        public Address(Country country, string city, string street, int number, string postalCode)
        {
            Country = country;
            City = city;
            Street = street;
            Number = number;
            PostalCode = postalCode;
        }
    }
    

    trying to add migration I get:

    PM> Add-Migration "Add_Address" System.InvalidOperationException: The entity type 'Address' requires a primary key to be defined. at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message) at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model) at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.LazyRef1.get_Value() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) 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.OperationBase.<>c__DisplayClass3_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) The entity type 'Address' requires a primary key to be defined.

  • User Avatar
    0
    rafalpiotrowski created

    I am not adding it to DbContext!

  • User Avatar
    0
    alper created
    Support Team

    Hi again,

    Bad news! Entity Framework Core does not support complex types at the moment. Good news! It will be supported in 2.0.0 version. When it releases we will update the framework.

    See this for more information

  • User Avatar
    0
    rafalpiotrowski created

    <cite>alper: </cite> Hi again,

    Bad news! Entity Framework Core does not support complex types at the moment. Good news! It will be supported in 2.0.0 version. When it releases we will update the framework.

    See this for more information

    I am using EFCore 2.0 now and still not able to use ComplexTypes!!! Is there a specific way to use it?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It seems like it only spoorts fluent configuration, have you tried that ? <a class="postlink" href="https://github.com/aspnet/EntityFrameworkCore/issues/246#issuecomment-323557172">https://github.com/aspnet/EntityFramewo ... -323557172</a>

  • User Avatar
    0
    rafalpiotrowski created

    It's working. Had to add some code into the DbContext in order for the migrator to recognize the complextype.