Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

Change database name created by Code First

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Apr 2012CPOL 30.9K   3   2
Let's see how to define our own database name using Entity Framework Code First

Introduction

If you are trying to use Code First, you should have seen the database name used by default is not always very useful and you surely want to rename it to use something easier. To do that, you simply have to modify your Context class to use the name you've choosen. Here is a simple MyContext class:

C#
public class MyContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<Task> Tasks { get; set; } 

    public MyContext()
    {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Task>().HasRequired(t => t.Project).WithMany(
              p => p.Tasks).HasForeignKey(t => t.ProjectId).WillCascadeOnDelete(false);
        modelBuilder.Entity<Task>().HasRequired(p => p.AssignedTo);
        modelBuilder.Entity<Person>().Property(d => d.BirthDate).HasColumnName("Birth");
        modelBuilder.Entity<Project>().Property(p => p.Name).HasMaxLength(255).IsRequired();
        modelBuilder.Entity<Project>().HasRequired(p => p.Manager);
    }
}

This code will generate a database named EFFirstSample.MyContext. To change the database name I just do this little tweak:

C#
public class MyContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<Task> Tasks { get; set; } 

    public MyContext():base("EFSample")
    {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Task>().HasRequired(t => t.Project).WithMany(p => p.Tasks).HasForeignKey(t => t.ProjectId).WillCascadeOnDelete(false);
        modelBuilder.Entity<Task>().HasRequired(p => p.AssignedTo);
        modelBuilder.Entity<Person>().Property(d => d.BirthDate).HasColumnName("Birth");
        modelBuilder.Entity<Project>().Property(p => p.Name).HasMaxLength(255).IsRequired();
        modelBuilder.Entity<Project>().HasRequired(p => p.Manager);
    }
}

If I run my application, a new database will be created, named EFSample. It's very simple, there is nothing more to add or to do. Hope it helps.

History

  • April, 2012 : First post.
This article was originally posted at http://drsn.net/7t

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
France France
I'm coding in .Net since 9 years, most with ASP.Net and SharePoint and a little using WPF, MVC, Windows Phone 8 and WinRT technology.
I have learned so much reading others experience and tutorials, or tips so I try to do the same, keeping learning from others of course.
You can also find my blog here : http://sharemstips.wordpress.com/

Comments and Discussions

 
QuestionThanks. Pin
User 1143895623-Feb-15 5:42
User 1143895623-Feb-15 5:42 
GeneralA very useful tip Pin
asprengard21-Nov-12 22:24
asprengard21-Nov-12 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.