Wriju's BLOG

.NET, Cloud and everything

CodeFirst EF 4.1 : Changing Database Table and Column name

At times we need to control the Table and Column name of our generated database or have different Entity/Property name of code than actual database in EF 4.1 Code First. There are two ways we can do it.

Using Annotations

using System.ComponentModel.DataAnnotations;

    //Changing database Table name to Employee
    public class Emp
        //Changing database column name to EmpId
        public int Id { get; set; }
        public string Name { get; set; }

Using FluentAPI

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    //Changing Database Column name to EmployeeId
        .Property(p => p.Id)

    //Changing Database table name to EmployeeData
Note: If you have both Annotation and FluentAPI available, the FluentAPI will win.