14

I want to add all tables in the database with a prefix like 'pe_', then the mapping between a class and a table will be like this: Category(pe_Category), Product(pe_Product), etc.

I know that with one single map, i can do like this:

[Table("pe_Category")]
public class Category
{
    public int CategoryId { get; set; }
}

But I don't like it cause there maybe have hundreds of entities.

So I'm finding a way to add the prefix globally, just like this:

public class Category
{
    public int CategoryId { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
}

// Global config, will affect all entities
table.Name = "pe_" + Class.TypeName ;

Anybody can help me?

RongieZeng
  • 784
  • 1
  • 7
  • 15

4 Answers4

27

Now I've find a solution with entity framework 6 alpha:

1) Create a class named "DefaultTableConvention":

public class DefaultTableConvention
: IConfigurationConvention<Type, EntityTypeConfiguration>
{
    public void Apply(
        Type type,
        Func<EntityTypeConfiguration> configuration)
    {            
        configuration().ToTable("PE_" + type.Name);
    }
}

2) In the DbContext, add the code below:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Add<DefaultTableConvention>();
    }

And that's all, It will affect all entities added in the DbContext. Detail:http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions

Update: Now with EF6, there's an easier way, which is called "Lightweight Configuration", here's the code:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Types().Configure(entity => entity.ToTable("PE" + entity.ClrType.Name));
    }
Enrique Carro
  • 380
  • 1
  • 3
  • 15
RongieZeng
  • 784
  • 1
  • 7
  • 15
14
protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     modelBuilder.Entities().Configure(entity 
      => entity.ToTable("PE" + entity.ClrType.Name));
 }

only works on ef6-beta-1; Microsoft changed Entities to Types from ef6-rc1

http://blogs.msdn.com/b/adonet/archive/2013/08/21/ef6-release-candidate-available.aspx#10444012

protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     modelBuilder.Types().Configure(entity 
      => entity.ToTable("PE" + entity.ClrType.Name));
 }
Bowyee
  • 311
  • 3
  • 3
  • I have tried your second code example, but I get errors if I leave off the 'base.OnModelCreating(modelBuilder);' However, if I put that in, either before OR after the types configuration line, my tables don't change names in the scaffolding when I Add-Migration.I'm trying to change the name of the AspNet tables to add a new prefix. They just stay the same though. – damccull Sep 07 '14 at 01:20
10

This works with EF Core 3.1+

Add a reference to the package Microsoft.EntityFrameworkCore.Relational.

foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
{
   entity.SetTableName("PE" + entity.GetTableName());
}
Darbio
  • 11,286
  • 12
  • 60
  • 100
BeardinaSuit
  • 889
  • 7
  • 21
  • 1
    To get this working with EF core 3.1 you need to add a package reference to `Microsoft.EntityFrameworkCore.Relational` – Darbio Dec 29 '19 at 10:49
3

This works for EF Core 2.0+

foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
{                        
    entity.Relational().TableName = "PE" + entity.Relational().TableName;
}
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162