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.<>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_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)
-
0
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... }
-
0
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.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 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_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. -
0
I am not adding it to DbContext!
-
0
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.
-
0
<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.
I am using EFCore 2.0 now and still not able to use ComplexTypes!!! Is there a specific way to use it?
-
0
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>
-
0
It's working. Had to add some code into the DbContext in order for the migrator to recognize the complextype.