Skip to content

How to add schema name to entity names? #553

Closed

Description

@ClassyCircuit

This is not a bug, this is a question - how do I include the schema name when generating entities?
For example, Prd.Store is currently generated as Store.cs, but I want it to be named Prd_Store.cs

How do I accomplish this? And I don't really want to specify each table manually, since I have over 400 tables that should follow this pattern: {schema}_{tableName}.cs

Activity

  1. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    I think you can use the renaming feature: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#custom-renaming-with-efptrenamingjson (combine "UseSchemaName" with a regex)

  2. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    Also, having 400 tables in a single DbContext will cause you pain.

  3. ClassyCircuit commented on Nov 3, 2020

    @ClassyCircuit
    Author

    They are split across multiple contexts, so it's more like 40-50 tables per one dbcontext.

    What does the "UseSchemaName" flag do? I don't see any examples where it's being used.

  4. ClassyCircuit commented on Nov 3, 2020

    @ClassyCircuit
    Author

    I don't understand how to use the regex feature, I followed the example in linked issue, and this is what I came up with:

    [
      {
        "UseSchemaName": true,
        "SchemaName": "Fact",
        "TableRegexPattern": "^.+$",
        "TablePatternReplaceWith": "Fact_$0"
      }
    ]
    

    But it doesn't rename anything.

  5. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    It includes the schema name in the property name - try it out!

  6. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    Start with the example in the linked PR

  7. ClassyCircuit commented on Nov 3, 2020

    @ClassyCircuit
    Author

    I used the sample that user e-ngin wrote 4 days ago:

    {
      "UseSchemaName": false,
      "SchemaName": "dbo",
      "TableRegexPattern": "^.+$",
      "TablePatternReplaceWith": "$0Entity"
    }
    

    I just replaced "dbo" with "Fact", and also added the missing square brackets, so in the end it looks like this:

    [
      {
        "UseSchemaName": true,
        "SchemaName": "Fact",
        "TableRegexPattern": "^.+$",
        "TablePatternReplaceWith": "$0Entity"
      }
    ]
    

    Then I regenerated entities, but they weren't renamed. Am I missing anything in the file?

  8. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    There could be a bug, please use the exact same parameters as in the PR first.

  9. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    If you still encounter issues, please share your renaming json file, and a sample create table script.

  10. ClassyCircuit commented on Nov 3, 2020

    @ClassyCircuit
    Author

    I got it working :)
    The issue was this flag, I had to disable it:
    image

    And if anyone else in the future needs this functionality, here is the config file I used:

    [
      {
        "UseSchemaName": true,
        "SchemaName": "Prd",
        "TableRegexPattern": "^.+$",
        "TablePatternReplaceWith": "Prd$0"
      },
      {
        "UseSchemaName": true,
        "SchemaName": "Sales",
        "TableRegexPattern": "^.+$",
        "TablePatternReplaceWith": "Sales$0"
      },
      {
        "UseSchemaName": true,
        "SchemaName": "Order",
        "TableRegexPattern": "^.+$",
        "TablePatternReplaceWith": "Order$0"
      }
    ]
    

    The only issue I am still encountering - after having disabled that flag, the tables are no longer named as they are in the database - the casings are now CamelCase, and underscores are removed. Which I don't really like, I prefer having 100% the same table name, only with prepended schema name to it.
    Even if I explicility add the underscore in the "TablePatternReplaceWith", it is still removed afterwards.

    But if that is difficult to fix, then I can live with it.

    @ErikEJ thanks for the assistance!

  11. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    Great. I think the database names issue may be fixed in EF Core 5

  12. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    Where is the _ by the way?

  13. ClassyCircuit commented on Nov 3, 2020

    @ClassyCircuit
    Author

    If you mean the regex part, then I added the underscore here:
    "TablePatternReplaceWith": "Order_$0"
    Result: "OrderItem.cs", should have been: "Order_Item.cs"

  14. ErikEJ commented on Nov 3, 2020

    @ErikEJ
    Owner

    I have linked your sample from the docs...

  15. tafs7 commented on Mar 12, 2021

    @tafs7

    @ErikEJ - Can renaming with regex patterns be used for all navigations?

    For example, every time we see a navigation property that would be generated as this:

    public virtual Payor PayorCompany { get; set; }

    instead we rename to simply this:
    public virtual Payor Payor { get; set; }

    I can do this one by one in the efpt.renaming.json file for each table in the "Tables[]" array, like this:

    [
        {
          "SchemaName": "dbo",
          "Tables": [
            {
              "Name": "Table1",
              "Navigations": [
                {
                  "Name": "PayorCompany",
                  "NewName": "Payor"
                }
              ],
              "NewName": null
            },
            {
              "Name": "Table2",
              "Navigations": [
                {
                  "Name": "PayorCompany",
                  "NewName": "Payor"
                }
              ],
              "NewName": null
            },
           //.....  repeat for each table where this might apply.....
          ]
       }
    ]

    but would be nice to just tell the tool to apply that renaming rule for EVERY table.

  16. ErikEJ commented on Mar 12, 2021

    @ErikEJ
    Owner

    Suggest you post a new issue with that idea!

  17. tafs7 commented on Mar 12, 2021

    @tafs7

    suggested - #848
    Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions