How to force your database name using Entity Framework migrations in a multi-project solution

If you're using Entity Framework migrations in a multi-project solution you might have noticed that when you run the 'Update-Database' action your database was given the name 'yourdatabase.Data.DataContext'. Some people don't mind this default naming scheme but I want to call my databases something a bit cleaner.

My typical MVC solution contains many projects to help keeps things separated. My database code is in my 'Data' project but my web.config file handling my connection string is usually contained in my 'Web' project. I noticed that even if I have given my database a name in my connection string this was not being picked up by the Entity Framework Migrations code.

Luckily all you have to do to force entity framework to use the database name you want is put the database name into your Context file. For example:

public class DataContext : DbContext
        public DataContext() : base("DatabaseNameHere")
        { }

        public DbSet Table1 { get; set; } 
        public DbSet Table2 { get; set; }

blog comments powered by Disqus

Get In Touch

Follow me online at TwitterFacebook or Flickr.

You can also contact me by email.

Buy Me A Coffee

Latest Tweets

Buy Me A Coffee