-
Notifications
You must be signed in to change notification settings - Fork 3.3k
HasOne/HasMany used with single string can be confusing #9171
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Comment from @divega: I agree that is confusing 😞 The fact that the type isn't being specified as a generic argument but is still needed is very easy to miss. I wonder if this is the only API in which this happens. If we believe it is worth addressing it, something like this may help:
modelBuilder.Entity<Samurai>().HasOneOfType("Entrance", navigationName: "Entrance")
.WithOne(); I think it helps because it reads like the (first) argument is the type. This looks complicated though: modelBuilder.Entity<Samurai>().HasOneOfType(typeof(Entrance), navigationName: "Entrance")
.WithOne(); Maybe we do it only for the one that works with strings? Perhaps there is a better solution. |
@divega A few of things I have been thinking about:
|
|
@smitpatel In the example, Samurai has a property called Entrance, which is of type Entrance. So the type of the navigation property tells us the type of the related entity. |
Filed #9191 for the generic overload. For the rest, putting in 3.0 as a possible breaking change that we could make at that time. Note that the type name has to be a full entity type name, and hence the code above while passing in OnModelCreating will fail at model validation time, which may make it easier to change the behavior without breaking any real code, at least once re-compiled. |
If the single string overload is changed to mean navigation it needs to retain old behavior when called on a shadow entity type to ensure old snapshots aren't broken. New snapshots can be generated to always call the two parameter overload. |
Maybe we need a way to determine that we are building a snapshot--e.g. by setting some flag. That way we could special case places like this. |
We can use the |
Also replace |
Issue #9171 Now if these methods are called with a single string, then that string represents the navigation property name. If there is no CLR property with the given name, then an exception is thrown. The exception is when the entity type has no CLR type, in which case the old behavior is preserved so as not to break existing model snapshots. Entity types with no CLR type are only valid in model snapshots.
Issue #9171 Now if these methods are called with a single string, then that string represents the navigation property name. If there is no CLR property with the given name, then an exception is thrown. The exception is when the entity type has no CLR type, in which case the old behavior is preserved so as not to break existing model snapshots. Entity types with no CLR type are only valid in model snapshots.
See discussion on #6674
If a relationship is configured like this:
it looks like it should be using the navigation property "Entrance", which may be private. But really it is the related entity type name:
The correct way to configure it is something like:
The text was updated successfully, but these errors were encountered: