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?
Open your database in PhpMyAdmin. Click on the database name in the menu to the left to unfold all tables. Select all tables that start with wp_; you should have 12 in total. Click With selected to open the drop-down menu and select Replace table prefix.
For those developers, Entity Framework has a modeling workflow referred to as Code First. Code First modeling workflow targets a database that doesn't exist and Code First will create it. It can also be used if you have an empty database and then Code First will add new tables to it.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With