Entity Framework 5 First Steps-Mapping

In previous article I’ve explained how simple is starting to use EF5 to access a database and use Database-Migrations to create a database suitable to contain your entities (with a simple Update-Command from the package manager console). When you show this technique to programmers the first complain they have is “ the structure of the generated tables is not suitable to me ”.

image

Figure 1: Database generated with previous code example

Suppose you want to specify maximum length of the various columns, specify not null columns and you want them to be named with a convention (yes, I know, still in 2012 DBA rules :)), finally you do not want to use Identity columns , because all customer you will save in this database comes from a legacy CMR application and you want to be able to use the very same id of the CRM system.

If you want to customize how the table is generated but you want to be completely free on how you name entities properties or context dbSet you need to specify a custom mapping that does not use standard convention over configuration. First you must create a mapping class for your Customer class (you can also do this with Attributes) to specify how properties will be mapped on database tables and columns.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);
        // Properties
        // Table & Column Mappings
        this.ToTable("Customer");
        this.Property(t => t.Id)
           .HasColumnName("cust_Id")
           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(t => t.Name)
           .HasColumnName("cust_Name")
           .HasMaxLength(100)
           .IsRequired();
        this.Property(t => t.Address)
           .HasColumnName("cust_Address")
           .HasMaxLength(200);
    }
}

You can put this class wherever you want, I usually put it into a subfolder named Mappings and I usually call this class with the same name of the entity with the suffix Map. Now you should only specify in your DbContext that this mapping is available.

1
2
3
4
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new CustomerMap());
}

By overriding OnModelCreating you can add your custom mapping to the Configurations collection , now if you issue another Update-Database you can verify that the table is now created the way you want.

image

Figure 2: New structure of the table that respect the CustomerMap class

Now you can see that the table is called Customer, columns are named with your convention and length and not null are created correctly. This is really good because if the DBA or some convention impose you some naming rules on database tables and column you are free to use business-friendly names in your entities.

Older post on EF5:

Code sample can be found here.

Alk.