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

Feature request: override PK property name to be always be Id #1222

Closed
ducman opened this issue Dec 16, 2021 · 7 comments
Closed

Feature request: override PK property name to be always be Id #1222

ducman opened this issue Dec 16, 2021 · 7 comments
Labels
question Further information is requested

Comments

@ducman
Copy link

ducman commented Dec 16, 2021

I have an established (old) DB where the PK of each table is named: {TableName}Id

It would be nice to have a feature to reverse engineer to entities with the property named Id which maps to the table's PK. This helps keep things a bit consistent for us.

Of course, if the table PK is already named Id, there is nothing to do. Same when encountering a table with composite PKs.

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 17, 2021

You should be able to use the regex based renaming with individual override where needed.

@ducman
Copy link
Author

ducman commented Dec 17, 2021

Sorry, not really sure what you mean by this.

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 18, 2021

I mean this

@ErikEJ ErikEJ closed this as completed Dec 20, 2021
@ducman
Copy link
Author

ducman commented Dec 20, 2021

Thanks for this. I'm unable to make use of regex since I have no clue what the table/entity name is to match it on the column name when wanting this done for all tables/entities.

Since I only have a few tables to deal with, I manually listed all out with the PK rename.

Still, it will be nice to have this done across all tables somehow. Unless you have some tricks up your sleeves.

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 20, 2021

You said they started with some letters and ended with Id...

@ducman
Copy link
Author

ducman commented Dec 20, 2021

Yes but only for the PK column.

To avoid hand jamming everything, I wrote the below query (on Sql Server 2016 or later) to generate the efpt.renaming.json file. It doesn't work with composite PKs; I'll have to tweak it when I run into that scenario. But this will do for now.

SELECT
    schemas.name AS SchemaName,
    Tables.Name,
    Columns.Name,
    'Id' AS NewName
FROM 
    sys.tables AS Tables
    JOIN sys.schemas
        ON schemas.schema_id = tables.schema_id
    JOIN sys.indexes
        ON indexes.object_id = tables.object_id
        AND indexes.is_primary_key = 1
    JOIN sys.index_columns
        ON index_columns.object_id = indexes.object_id
        AND index_columns.index_id = indexes.index_id
    JOIN sys.columns AS Columns
        ON columns.object_id = tables.object_id
        AND columns.column_id = index_columns.column_id
FOR JSON AUTO

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 20, 2021

Excellent, I will add that to the docs!

@ErikEJ ErikEJ added the question Further information is requested label Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants