-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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] attribute does not play nicely with lazy loading and nullable references in C# 8.0 #15876
Comments
Also, because of the problem I described above, it is impossible to initialize the owned object on an entity - you get the error saying that entity cannot be added because it has the navigation set (where navigation is really an owned property). |
@ibasin There's a few things going on here. First, just because something is owned does not mean it is not an entity type. It therefore follows from this that the Second, it's involvement in lazy loading is something that is not required: see #12462 Third, the implications of this for non-nullable references is interesting. We will discuss this. |
@ajcvickers Thank you for a quick reply.
System.InvalidOperationException: 'Navigation property 'Address' on entity type 'Author' is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.'
I think both of these are real problems with no easy workarounds. Thank you! Please let me know if I could be of service. Ilya |
@ibasin The 'workaround' for #12462 is to make the property virtual. Is there a reason you can't do this? We discussed the non-nullable reference type scenario, but came to the conclusion that it is really no different from any other navigation property, which is discussed on #15520 and #10347. The way to handle this is to use There is also a more general issue here, which is that owned entity types are not value objects and don't have the same semantics as value objects. Attempting to use these types as if they are value objects often ends up being difficult and feeling wrong because of the very different semantics. |
There is still a problem. Data for an inline owned property cannot be seeded. I see no workaround. Am I missing something? Thank you. |
I am using Visual Studio 2019 Preview, .Net Core 3.0 Preview 5 and EF Core Preview 5.
If a class is marked [Owned] and lazy loading is enabled, Lazy loading thinks this is a navigational property. This causes two problems:
Steps to reproduce
Create a .Net Core Console application. Reference EF Core and Lazy Loading NuGets.
Here is the entity and the owned type
Attempting to seed the data for this class in OnModelCreating method will always fails because it thinks that a nav property Address is initialized (and it will always be because it is not nullable):
'The seed entity for entity type 'Author' cannot be added because it has the navigation 'Address' set. To seed relationships you need to add the related entity seed to 'USAddress' and specify the foreign key values {'AuthorId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.'
Further technical details
EF Core version: 3.0.0-preview5.19227.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 Preview
The text was updated successfully, but these errors were encountered: