Naming the Database Created by EF Code First

If you don’t want to have a database name like:
holy.crap.this.is.a.long.name.with.my.whole.namespace.contextname
…then you simply need to name the database.  To do this, we need to create the class that inherits from DbContext and call the base constructor to pass in the name.
    public class FamilyContext :

DbContext        {        public FamilyContext (): base(“FamilyContext”) {}        }

 

Sweet.  So, one other thing to pay attention to then, especially if you’re using MvcScaffolding or other similar tools.  If you’ve named your context you’ll need to tell it where you want your entity collections to manifest.  For MvcScaffolding in particular, you can use the –DbContext switch and pass in your context name.  That looks something like this:
Scaffold Controller Family -DbContext Family

The result is that MvcScaffolding will now look for that type in the target project and add the appropriate code.


    public class FamilyContext : DbContext
{
public FamilyContext (): base(“FamilyContext”) {}
public DbSet<MyProject.Models.Family> Families { get; set; }
}


Hope this helps!