Remove tableprefix with .net Core

Viewed 667

We make a .net Core application. For that we have to use an existing Database. This database is used from other programs too, so we cannot change something on it. So it's database first. The problem is, that we have some prefix in our tables. The tables always starts with tbl and the fields start with an individual prefix:

Tablename: tblPerson

Fields: PeName, PeStreet, ...

So if I use PowerTools the classes always have this prefixes. Because our database is quiet big, we use the powertools everytime, when we need a new table form the db. Because of that, we can't change them directly in the DbContext.

Can I remove the prefix somehow? Or change them globaly in my solution?

5 Answers

You can have different model class names and property names to the database table name and column name.

Use the Table and Column attributes to tell EF core the names in the database.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("tblPerson")]
public class Person
{
    [Column("PeName")]
    public string Name { get; set; }

    [Column("PeStreet")]
    public string Street { get; set; }

    // ...
}

You can also you can use the Fluent API to define these properties in OnModelCreating in your DbContext.

Data annotations documentation

You can rename tables and columns during the reverse engineering process by adding a file called efpt.renaming.json in the project folder. Below is the format of this file:

[
  {
    "UseSchemaName": true,
    "SchemaName": "machine",
    "Tables": [
      {
        "Name": "alert_ui",
        "Columns": [
          {
            "Name": "alarm_name",
            "NewName": "NameOfTheAlarm"
          },
          {
            "Name": "columnOldName1",
            "NewName": "columnNewName1"
          }
        ]
      }
    ]
  },
  {
    "UseSchemaName": true,
    "SchemaName": "master",
    "Tables": [
      {
        "Name": "measure",
        "Columns": [
          {
            "Name": "alarm_name",
            "NewName": "NameOfTheAlarm"
          },
          {
            "Name": "columnOldName1",
            "NewName": "columnNewName1"
          }
        ]
      }
    ]
  },
  {
    "UseSchemaName": false,
    "SchemaName": "dbo",
    "Tables": [
      {
        "Name": "work_cells",
        "NewName":  "WorkCell",
        "Columns": [
          {
            "Name": "columnOldName1",
            "NewName": "columnNewName1"
          }
        ]
      }
    ]
  }
]

And the link to this file on github : https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/EFCorePowerTools/efpt.renaming.json

you can remove table / column prefixes using regex pattern in efpt.renaming.json file:

[
  {
    "UseSchemaName": false,
    "SchemaName": "dbo",
    "TableRegexPattern": "^(tbl|vw)",
    "TablePatternReplaceWith": "",
    "ColumnRegexPattern": "^(tbl|vw)",
    "ColumnPatternReplaceWith": ""
  }
]

you can use Fluent API to configure an entity to map it with database table.

  public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Configure default schema
            modelBuilder.HasDefaultSchema("Admin");

            //Map entity to table
            modelBuilder.Entity<Student>().ToTable("StudentInfo");
            modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo");
        }
    }

I will just add to @extragen answer. That approach works but only in case you have unechecked Use table and column names directly from the database in EFPT UI here:

EFPT UI

or in the file efpt.config.json set value:

"UseDatabaseNames": false

because if it is true those renaming changes wont be applied.

Related