-
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
Revisit navigation naming convention when reverse-engineering composite keys #16711
Comments
To add more information, I feel this is a bug due to the way db-first scaffold names the property [ParentTableName]Navigation when it should name it [ChildTable]Navigation. If you have a composite FK from parent to child on say 2 fields why would it name the "Navigation" property prefixed with the parent table and not the object it is navigating to? In this example Entity is the parent and the EntitySubType is the child with the composite FK, mainly for data integrity reasons, although I was not sold on this concept, came from the DBA that way. |
There has to be a way it can use the child name + "Navigation", you know something that would make more sense from the API side when using the models logically. Right now it would seem every time I scaffold I have to rename a bunch of properties. |
@cgountanis First, if you are using EF Core 2.0, then you should upgrade. That release has been out-of-support for some time now. Following that, if you are still seeing issues, then please post the tables and the generated classes so we can fully investigate. |
Sorry I should have been more clear. Easy to reproduce, make a parent table and a child table with composite key (2 columns PK on parent > child as FK), then do database-first scaffold.
The models when it creates the virtual property, the property name is backwards. In Parent model: Named ParentTableNameNavigation when it should be ChildTableNameNavigation, to make any sense. Plus, if you ever had 2 children with comp from same parent, my guess is it would really get funky. Have not tested that yet. |
Please post the CREATE TABLE statements and the generated model code (DbContext and POCO classes) |
Notice the ParentTable "Book" virtual property named as BookNavigation even though it is of type BookType. The reason for this FK is to make sure the parent table (in this example Book) can not set TypeId/SubTypeId without it being in the BookType child table for obvious reasons. |
I made sure I was 100% up-to-date on the latest stable version just to make sure I did my part here. Microsoft Visual Studio Professional 2019 Version 16.2.0
Same results, though. Sorry I thought I was on the latest and greatest (stable). |
For the case of composite key, the dependent to principal navigation name is generated using common prefix of foreign key properties. It is common practice to write the code that way. In your case it turns out to be Book. Since the class itself is named book, it added BookNavigation. |
The FK is from Book > BookType, look at the |
I believe an example would clarify. public class Blog
{
public int Id1 { get; set; }
public int Id2 { get; set; }
}
public class Post
{
public int Id { get; set; }
public int BlogId1 { get; set; }
public int BlogId2 { get; set; }
public Blog Blog { get; set; }
} Above is the general pattern we recognize. Of course people can write their code according to their preference. We follow heuristic to arrive at the name. You are free to change it to whatever you prefer. |
To add, we have solved this by NOT using FK and just using CHECK CONTRAINTS within the DBMS so EF can stay happy. I posted this in an effort to shed some light on Composite Keys property naming conventions as it would seem from my examples you would not name the property the parent but the "TYPE" it is representing. |
Triage: we will move this to the backlog to consider changing the default pattern. /cc @ErikEJ in case he has thoughts. |
I would appreciate it I think other people with composite keys will run into something like this at some point database first. |
The point should not be to "change the default pattern". The point should be to enable users to control the pattern. I gave up on using EFCore and have been pounding the table at work for us to move to either EF6 or NHibernate. For any reasonably complicated database architecture, the pattern-based approach tightly couples you to a specific version of EFCore. It is not even guaranteed the patterns wont break from release to release, making you stuck without an upgrade path and piles of EFCore linq queries to port to fix the issue. |
That is a shame. We have been using EF Core for simple to complex designs and it overall has been working well. Very fast and it really simplifies the business data layer for us personally. As long as your DB design is solid, be it relational or complex relational (transaction design), it DOES work. Maybe revisit it in a few months after a couple 3.0 releases. Sounds like the 3.0 release will really help, including the SqlFrom for people that must use SPROCS, etc... Would be nice if it supported views but I would much rather use the relational tracking object model approach honestly. (KISS) Let's be honest database-first was never a priority and I am sure it will come around. Composite keys mentioned my post are replaced with CHECK CONSTRAINTS because it was not really even needed for the parent > child connections, more of a constraint issue anyway for good data. |
I have a database first solution core 2.2.4. Scaffolding does work and it creates a navigation property. The issue I'm having is how the scaffolding names that property. It causes parent table navigation when really it should call it the child table navigation. What if for some reason there was two composite keys. It wouldn't be able to use the same name.
Is there any way to customize the name does it use something from the foreign key description or anything where it would name the navigation property that it automatically creates a little more smartly?
The text was updated successfully, but these errors were encountered: