Customising Entity Framework Code-First

 Jun 21, 2016

In a previous blog post we compared the different approaches to create a Data Access Layer to MVC applications using the Entity Framework. In particular we went into some of the details of how to use the Code-First approach. In today’s blog we move one step further and show you what to do when there is a mismatch between the schema of the database and the classes you are using to model the business entities.

Entity Framework Code-First uses conventions to name tables and columns and to identity which property should be used as a primary key. But in many scenarios you already have a database and you don’t want to use the same naming conventions of the database schema with your entity classes.

One approach to solve this mismatch problem is to use Data Annotations as we show next.

Start by adding the following namespaces to your class definition:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

Now let’s say you want to map a class with the name Customer with a table named tblCustomers2015. By default EF would expect a table with the name Customers, the plural form of your class name. In this case you have to use the Table annotation as follows

[Table(“tblCustomers2015”)]
public class Customer
{ … }

For the primary key EF expects a match to ID or CustomerID. It’s not case sensitive. If there is a mismatch you just use the Key attribute.

[Table(“tblCustomers2015”)]
public class Customer
{
   [Key]
   public string AccountNumber { get; set; }
  …
}

In case of a composite key you can apply the Key attribute to more than one column, but in this case you also need to acknowledge the order of the key properties.

[Table(“tblCustomers2015”)]
public class Customer
{
   [Key]
   [Column(Order = 1)]
   public string TerritoryId { get; set; }
   [Key]
   [Column(Order = 2)]
   public string AccountNumber { get; set; }
  … }

There are a few more data annotations which are more targeted to the scenario where you don’t have a database and EF will create one for you with the necessary customisations.

To learn more about using the EF in MVC applications we recommend the course 20486 - Developing ASP.NET MVC 4 Web Applications.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil's and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top
Back to top