RSS

Tag Archives: code first

Rename Table and Column Name in EF Code First

Business rules change over the time. For developers, this can be frustrating. Especially after you have spent enormous amount of time to name your objects properly. After all, we all know naming is the most prominent process of the development.. 🙂

Luckily, in Entity Framework, you can change table names quite easily.

Two ways, using data annotation and Fluent API. (Code is in Entity Framework 6)

Data Annotations

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

// Change table name to People
[Table("People")]
public class Employee
{
    // Change column name to PersonId
    [Column("PersonId")]
    public int Id { get; set; }
    public Guid DepartmentId { get; set; }
    public int CompanyId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

FluentAPI

Context file (inherit from DbContext)

using System.Data.Entity;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Change column name to PersonId
    modelBuilder.Entity<Employee>()
        .Property(p => p.Id)
        .HasColumnName("PersonId");

    // Change table name to People
    modelBuilder.Entity<Employee>()
        .ToTable("People");
}
Advertisements
 
Leave a comment

Posted by on June 18, 2015 in General

 

Tags: , ,

Composite Key (Multi Columns Primary Key) in EF Code First

Composite Key is basically Primary Key that spans multiple columns. Look like this:

composite-key-multi-columns-primary-key-in-ef-code-first-1

composite-key-multi-columns-primary-key-in-ef-code-first-2

To achieve this in EF Code First, all you need to add is Key and Column data annotation. (I am using Entity Framework 6).

Column attribute takes in Order parameter which you can use to specify the order as it appears on the table.

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

public class Employee
{
    [Key]
    [Column(Order = 1)]
    public int Id { get; set; }

    [Key]
    [Column(Order = 2)]
    public Guid DepartmentId { get; set; }

    [Key]
    [Column(Order = 3)]
    public int CompanyId { get; set; }

    public string Firstname { get; set; }
    public string Lastname { get; set; }
}
 
2 Comments

Posted by on June 11, 2015 in General

 

Tags: ,

Entity Framework Code First Migrations Commands

To enable migrations in your Entity Framework Code First project, the following commands guide you thru installation, enabling the migration, adding a migration and updating the database. All the commands are run on Package Manager Console.

Install EntityFramework package

PM> install-package EntityFramework

Enable Code First Migrations

PM> Enable-Migrations

Add a migration

PM> Add-Migration <migration_name>

Update the database

PM> Update-Database
 
Leave a comment

Posted by on June 11, 2013 in References

 

Tags: , ,

 
%d bloggers like this: