1
votes

I have in my database a lot of tables with the prefix "tbl", like tblCustomer, when generates the model using EF6 tool (Add->New Item->Data->ADO.NET Entity Data Model) and selecting Code First from database, EF generates all the Classes with the tbl as a prefix, how to edit the generation templates to take off those prefix?

Rowan Miller explained how to edit those generation templates in this post Customizing ‘Reverse Engineer Code First’ In The EF Power Tools but don't know how to do it with the new consolidated tools in EF 6

1
Remember.. if every table in your database starts with tbl its the same as if no table starts with tblcrthompson
@paqogomez definitely, but not all the tables have the prefix and the code is not readable at all (I mean is more readable Customer than tblCustomer, I think it the natural way), and eventually we can use that model with another databaseddieppa

1 Answers

1
votes

You might have an easier time simply changing the names of the classes and using DataAnnotations (look for the section called "Table and Column") to map to the correct tables after you generate your code first models.

using System.ComponentModel.DataAnnotations;

[Table("tblMyModel")]
public class MyModel {
    public int ID {get; set;}
    //etc
}

Alternatively you can use the FluentAPI as well.

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Entity<MyModel>()
        .ToTable("tblMyModel");
}

This of course may not be practical for large table structures, but it will work.

If you want to venture into the more advanced options that EF6 offers, you can read up on Code First Conventions.