0

When using EF4 to create a model, I would like to replace all underscores as well as table/column prefixes so that the model reads cleanly. Our database has a structure something like:

Table:ABC_Customer
-Column:ABC_Customer_Id
-Column:Name
-Column:ABC_Customer_Type_Id

Table:ABC_Customer_Type
-Column:ABC_Customer_Type_Id
-Column:Name

I would like to have entities names like so:

Entity:Customer
-Property:CustomerId
-Property:Name
-Property:CustomerTypeId
-NavigationProperty:CustomerType

and so on and so on.

When generating the edmx file, the EF designer names all the entities exactly as they appear in the database. I know this behavior can be modified using the T4 templates, but I just discovered that I can rename the entities in the designer, which generates EntitySetMapping elements within the edmx file, so T4 seems like overkill. It almost seems like I just want to post-process the edmx file, without using the T4 templates since after that replacement, I want the default behavior. Which approach is the most approriate and why?

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
mhildreth
  • 82
  • 7

2 Answers2

1

EDMX is responsible for mapping and any new names should be defined in mapping = in EDMX.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

I would use the out of the box functionality unless I was dealing with a very large number of tables. The time investment to get the T4 templates to do exactly what you need may not be worth it.

For a small database I would stick with the designer if doing database first development.

Alternatively, you could go code first to get a clean model like you want, point it at your existing database and handle the mappings in code for each object in the model:

public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
    public CustomerConfiguration()
    {
        Property(t => t.CustomerId).HasColumnName("ABC_Customer_Id");
        ...
    }
}

And then apply your custom configurations (i.e. mappings) to the model at the same time you change the table mappings:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new CustomerConfiguration());

    modelBuilder.Entity<Customer>().ToTable("ABC_Customer");
}

The more I work with it, the more I like code first. Julie Lerman's new book Programming Entity Framework: Code First is great!

John Laffoon
  • 2,885
  • 2
  • 26
  • 38
  • The database is farily large (100+ tables and 1200+ columns), so the designer will be tedious, and potentially error prone. Code first is not an option as the database has pre-existed for a long time. We've been using strongly typed datasets to date, but would like to switch to EF. – mhildreth Dec 21 '11 at 20:28
  • @mhildreth Code first **is** designed to work with existing databases. I'm not keen on navigating around a large model in the designer and mapping window trying to figure out what mappings I have. With the code first approach I know any mappings I have for the Foo table are in the FooConfiguration.cs file. – John Laffoon Dec 22 '11 at 03:47