1

I'm currently working on ASP.net MVC 4 framework and I intend to use the code first approach(since it seems more change-friendly and there's no need to change the database since the entity framework offers the migration tool), my question is, the entity framework creates automatically the database if it doesn't exist, is there a way to define the name of the database it creates? if so how is it? I suspect it is related with

<connectionStrings>
</connectionStrings>

I define on the Web.config file but so far I can't seem to understand its syntax.

TLDR; How can I define the name of the database when using code first approach and entity framework?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
oskar132
  • 794
  • 2
  • 12
  • 32

1 Answers1

1

first add your connection string and your db information. for more info visit this link. EF Code First

<configuration>
  <connectionStrings>
    <add name="yourConnectionstringName"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;"/>
  </connectionStrings>
</configuration>

in your Dbcontext class, pass the connectionstring name to the dbcontext base constructor like this.

public class BloggingContext : DbContext
{
    public BloggingContext()
        : base("name=yourConnectionstringName")
    {
    }
}
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83