Click here to Skip to main content
12,450,771 members (46,601 online)
Click here to Skip to main content
Add your own
alternative version

Stats

15.5K views
3 bookmarked
Posted

Change database name created by Code First

, 17 Apr 2012 CPOL
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)

Share

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/

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionThanks. Pin
Member 1147329023-Feb-15 5:42
memberMember 1147329023-Feb-15 5:42 
GeneralA very useful tip Pin
asprengard21-Nov-12 22:24
memberasprengard21-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 17 Apr 2012
Article Copyright 2012 by Nadege Rouelle
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid