Click here to Skip to main content
Click here to Skip to main content

Change database name created by Code First

, 17 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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:

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:

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.

License

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

About the Author

Nadege Rouelle
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/
Follow on   Twitter

Comments and Discussions

 
GeneralA very useful tip Pinmemberasprengard21-Nov-12 22:24 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 17 Apr 2012
Article Copyright 2012 by Nadege Rouelle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid