-
Notifications
You must be signed in to change notification settings - Fork 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
Clarify that FK shadow properties are usually created by convention #1016
Comments
The fluent API docs for relationships and FKs is here: https://docs.microsoft.com/en-us/ef/core/modeling/relationships |
Yeah I know. My request still stands. There's a page about shadow properties, which says what they're most useful for, but then fails a decent example.
WTF does that line of code do? It's not obvious to me. 99% of people won't bother to provide feedback when they struggle and will turn to google to find an answer. So when someone does provide feedback it shouldn't be disregarded. |
@mattfrear Trying to connect the dots here, maybe the confusion is that they are commonly used for FKs, but rarely explicitly defined for FKs. Hence showing API here to define an FK in shadow state would not be terribly useful since that's not a very common thing to do. Would it be clearer to say, "They are most often used for foreign key properties, where they are added to the model by convention when no foreign key property has been found by convention or configured explicitly." With regard to not knowing what that line of code does, can you be more specific about what is ambiguous? |
Yes, I think that would be an improvement.
I have re-read the page and now it makes sense, so please disregard my earlier comment. I landed on this page trying to solve a specific and frustrating problem (one to many relationship with a nullable int foreign key which should not be nullable), I was hoping this page would shed some light on my problem but I accept that I was perhaps looking in the wrong place. Sorry for being rude. |
@mattfrear Np |
I think that the example from https://docs.microsoft.com/en-us/ef/core/modeling/relationships#foreign-key should be used for the FluentApi paragraph.
|
How to seed data to shadow properties ? Continent model
Country model
App context
App data seeder
I need to insert continent_code's value while seeding data to database, |
@Yuant-tobing Use an anonymous type as shown in the docs. |
@ajcvickers One thing I've wished about MSFT Docs for years (going back to MSDN) was finer-grained breadcrumbs (# section anchors). In the Data Seeding docs, there is a hidden outline of user stories and one has to read through the web to untangle it all. What would be hyper-text-awesome is if I could see all "owned types" related documentation across multiple pages all at once. Miguel De Icaza had a glimmer of such hopes with how Xamarin managed documentation (comments separate from code), but it feels like it never became a butterfly from its chrysalis. It would probably also be nice if each example was a self-contained git repository, so that people could clone it in Github for Windows and just automatically play with it, fork it, submit bugs based on it. Perhaps not very actionable given all the things you must do, but if I never ask, you will never know. |
@mattfrear I think the key concept is that FK shadow properties are primarily an artifact of domain-driven design. DDD enthusiasts do not want to expose keys in their domain model, because they have zero practical consequence to testing or to the business domain. All the keys do is connect a graph of objects together.
I suspect this example is actually incorrect or only correct under certain conditions. If you are doing database-first with EFCore, you need to specify the type parameter, or the wrong overload of HasForeignKey will get selected by the .NET runtime. I say wrong loosely. The same problem happens in VisualStudio with IntelliSense because IntelliSense prefers auto-completing to the |
@jzabroski That issue is only present when calling |
Can someone please explain this line of documentation? "We recommend explicitly adding the shadow property to the model before using it as a foreign key (as shown below)." It's on this page: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#shadow-foreign-key It's extremely frustrating, because I am trying to reduce volume of code. There's no explanation of what would happen if you call the .HasForeignKey(""BlogForeignKey") method without having explicitly defined the Shadow Property beforehand. How do I know whether or not I can omit the explicit property addition? UPDATE: After understanding the problem better, I opened a separate issue about this: |
> They are most often used for foreign key properties
The Fluent API example on this page isn't much help. Can you provide an example that does something with relationships or foreign keys?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: