.NET Zone is brought to you in partnership with:

I'm an independent consultant working in Italy with more than 8 years of experience in designing and developing application for .NET framework, both in windows and Web environments. I'm particularly involved in Continuous Integration strategies, and designing infrastructure of applications, since I'm a lover of pattern and Application Lifecycle Management. I'm a great fan of Communities and I'm a co-founder of DotNetMarche, an Italian community focused on .NET development and I love blogging about technology. Ricci is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. View Full User Profile

Entity Framework 5 First Steps – Mapping

09.29.2012
| 9225 views |
  • submit to reddit

In a previous article, I’ve explained how simple it is 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 many programmers, the first complaint 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 a maximum length for the various columns, specify not-null columns, and you want them to be named with a convention. Finally, you don't want to use identity columns, because all the customers you will save in this database come 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.

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.

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.

 

Published at DZone with permission of Ricci Gian Maria, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)