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

Strong typing names in data annotations during reverse engineering where possible #32126

Closed
piskov opened this issue Oct 22, 2023 · 3 comments
Closed

Comments

@piskov
Copy link

piskov commented Oct 22, 2023

With data annotations you get db-first code like this:

[Index("Id")]
[PrimaryKey("Id")]
public class Post
{
    public int Id { get; set; }

    [ForeignKey("BlogKey")]
    public Blog Blog { get; init; }
}

It would be much more error-prone to use “strongly typed” nameof version when possible during reverse engineering:

  • PrimaryKey(nameof(First), nameof(Second)].
  • string interpolation for foreign keys [ForeignKey($"{nameof(First)}, {nameof(Second)}")].

This is especially useful if user would like to rename the property after scaffolding:

class Item
{
    [Column("ActualNameInDb")]
    public string Property {get; set;}
}

Also one could easily see “usages” of the property when it’s referenced not via a magic string but a nameof.

@piskov piskov changed the title Strong typing names in data annotations during reverse engineering were possible Strong typing names in data annotations during reverse engineering where possible Oct 22, 2023
@ajcvickers
Copy link
Member

Duplicate of #11744. See also #26622.

@piskov
Copy link
Author

piskov commented Oct 23, 2023

@ajcvickers oh, that’s rather unfortunate considering:

  1. [InverseProperty] is only needed when there is more than one relationship between the same types (which is far more rare).

  2. If you use pluralizing like with EF power tools, you would never end with ICollection<Item> Item { get; set; }Items will be used as a name instead and there will be no clash to begin with.

So it seems far more useful nameof was sacrificed for rather rare case (unless there’re others not mentioned in the referenced issue) and I would rather fix those manually if they would ever arise (not that they will seem to appear with pluralizing or not including the inverse attribute if there’s just a single relationship between the entities).

Maybe it is something that can be used at least as an option during scaffolding. Or even maybe implemented manually in T4? In our codebase we do replace all those magic strings manually to nameof.

cc: @ErikEJ

@ajcvickers
Copy link
Member

@piskov The general case is very difficult to solve--we would end up re-implementing significant parts of the compiler. If you're okay with limitations, then the T4 templates are where to do this.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2023
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

2 participants