简体   繁体   中英

Take off database table prefix when using ef6 code first from existing database

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM