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

get names by referance instead of hardcoding #35160

Open
hutonahill opened this issue Nov 20, 2024 · 3 comments
Open

get names by referance instead of hardcoding #35160

hutonahill opened this issue Nov 20, 2024 · 3 comments

Comments

@hutonahill
Copy link

Problem:

I am a newcomer to Entity Framework Core and just getting into using my Entity Framework Core models as the primary source for the schema of my database.

I initially created this model using dotnet ef, pulling the model from an existing database.

When modifying my schema I found that I encounter issues when renaming properties within an IDE. It would not catch the name within quotes of a decorator, for example:
[InverseProperty("apartments")]

If I change the property name of apartments that the inverse property is pointing to than I have to go though my model and change every string. this is very annoying.

IDEs have a solution for this. I am using Rider and I can just hit f2 and modify the name of a variable. but when dotnet ef creates a model it hardcodes these values in such a way that it's hard for an IDE to detect that that is supposed to be a reference to a variable name.

Solution:

C# has a solution: nameof. When dotnet ef wants to reference a class name it should be using nameof(structureTypes.apartments) instead of "apartments". This approach is much more data-driven and is easier on IDEs

@hutonahill
Copy link
Author

making this conversion in my code base manually, and you probably want to use nameof(Models.StructureTypes.apartments) as StructureTypes is a common name name for objects in the data model

@ajcvickers
Copy link
Member

@hutonahill We investigated using nameof before, and even tried it. It did not work out well--see ##26588. Scaffolding to use the model builder APIs will generally make the code easier for refactoring tools to understand.

@hutonahill
Copy link
Author

Glad I am not the first person to think of this.

Is this the issue you're referring to: #26588 (comment)? i don't understand the problem

I scaffold using data annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants