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

Owned Type is generating error for missing a primary key #10814

Closed
FrederickBrier opened this issue Jan 30, 2018 · 4 comments
Closed

Owned Type is generating error for missing a primary key #10814

FrederickBrier opened this issue Jan 30, 2018 · 4 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@FrederickBrier
Copy link

I created an owned type class and included it as a property within a class mapped to a database. I added two DataAnnotations to each of the 3 properties within the owned type class.

    [Required]
    [StringLength(80)]

EF Core is assuming that because there are DataAnnotations, that the owned class is a full table class. It then throws an exception about requiring a primary key - which makes sense. It seems like there needs to be an [OwnedType] DataAnnotation.

Do I have to put the Owned Type in the same source file, as per your examples?

System.AggregateException: One or more errors occurred. (The entity type 'Address' requires a primary key to be defined.) ---> System.InvalidOperationException: The entity type 'Address' requires a primary key to be defined.
    at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.SqliteModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__0(Object k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.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.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetDbConnection(DatabaseFacade databaseFacade)

Further technical details

EF Core version: 2.0.3
Database Provider: For the test which is failing SQLite (memory). For the App, not yet there - PostgreSQL.
Operating system: Windows 10 Pro
IDE: Visual Studio 2017 15.4

@ajcvickers
Copy link
Contributor

@FrederickBrier Can you post code for the entity type and owned entity type and the code in OnModelCreating that configures the owned type?

@FrederickBrier
Copy link
Author

My problem was that I was using EF Core 2.0.3. An OwnedAttribute (Annotation) was added on 2017-12-14 in a commit by AndriySvyryd. It does not seem to exist in 2.0.3. I did not see the tag for 2.0.3, but 2.0.2 was tagged 2017-10-13. So I switched to 2.1.0-preview1-28181 off of dotnet.myget.org and now am getting the exception described in both:

Support cascade updates for owned types/aggregates #10551
SaveChanges() throw exception, using OwnsOne in OnModelCreating() when initialize database #10578

The annotation only occurs once in the DataAnnotationTestBase.cs file, although obviously it is used in many tests. My entities are very similar and simple, except the classes are public, not protected. If you have any suggestions on debugging this problem, please let me know. This owned types functionality was announced, so I assume it is real, works, and the problem is my use of it. Thank you.

@ajcvickers
Copy link
Contributor

@FrederickBrier Owned types were released in EF Core 2.0, but with some limitations. At that time, configuration was through the fluent API only. OwnedAttribute has been added for EF Core 2.1, which is as of now only available in nightly builds.

If you think you are hitting a bug, then we will most likely be able to help if you post a runnable project/solution or code listing that demonstrates the issue. If you are asking how to use the feature, then documentation is being worked on in this PR: dotnet/EntityFramework.Docs#469

@FrederickBrier
Copy link
Author

It is now working with the preview release. Thank you!

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Feb 7, 2018
@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
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

2 participants