61

How do I tell EF what to name the database and where to put it?

If there is no connection string in the Web.Config, it tries to put it in the local SQLEXPRESS Server, but I want to put it out on a known SQL Server and name it what I want. Any suggestions?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Sam
  • 15,336
  • 25
  • 85
  • 148
  • 1
    You can name your database by chaining your context's constructor like this `public MyContext() : base("NameForTheDB") { }` – snippetkid Feb 26 '16 at 13:39

8 Answers8

64

Create a connection string in the app.config/web.config with the same name as the context and the EF will use that DB.

Jacob
  • 3,598
  • 4
  • 35
  • 56
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • 1
    Here is a link that explains all the different options: http://msdn.microsoft.com/en-us/data/jj592674 – Gabriel Aug 23 '12 at 03:20
25

How to Use a Different Connection String Name with EF

EF will use the name of the database in the connection string. When you want to decouple the name of your connection string from EF, you need to provide your connection string to the constructor. Example:

public class DatabaseContext : DbContext
{
    public DatabaseContext() 
      : base(ApplicationParameters.ConnectionStringName)
    {
    }

    public DatabaseContext(string connectionStringName)
      : base(connectionStringName)
    {
    }

}
ferventcoder
  • 11,952
  • 3
  • 57
  • 90
19

in Class :

public class Context : DbContext
{
    //SET CONNECTION STRING NAME FOR DataBase Name :
    public Context() : base("YourConnectionName") { }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

in web.config:

<connectionStrings>  
    <add name="YourConnectionName" connectionString="Data Source=A-PC\SQLEXPRESS;
    Initial Catalog=MyDataBase; Integrated Security=True" 
    providerName="System.Data.SqlClient" />
</connectionStrings>  

Thanks ferventcoder.
Ref => http://brandonclapp.com/connection-strings-with-entity-framework-5-code-first/

Amin Ghaderi
  • 1,006
  • 13
  • 22
8

Alternatively you can set the name in your DbContext constructor.

carlsb3rg
  • 752
  • 6
  • 14
  • 1
    Check this out: [Change Database Name Created by Code First](http://www.codeproject.com/Tips/367617/Change-database-name-created-by-Code-First) – Dänu Jul 23 '12 at 23:59
  • 1
    @BrunoSalvino `public DatabaseContext(): base("DatabaseName") {}` will do the trick. You don't have to have the connection string defined. – romanoza Apr 14 '16 at 09:36
5

As already mentioned, you can declare your connection string inside the config file of your application with a name (let's say "YourDBName") and then pass this to the DbContext base constructor call (I will add this to the answer for providing a complete answer - great answers already given on this).

Alternatively, you can set this programmatically in your DbContext Extension class, using the Database.Connection.ConnectionString property. For instance:

App.config:

<!-- More.... -->
<!-- You can do this in a declarative way -->
<connectionStrings>
  <add name="YourDBName"
       connectionString="<Your connection string here>"
       providerName="<Your provider here>" />
</connectionStrings>
<!-- More.... -->

DatabaseContext.cs:

public class DatabaseContext : DbContext
    //Link it with your config file
    public DatabaseContext () : base("YourDBName") 
    {
        //And/Or you can do this programmatically.
        this.Database.Connection.ConnectionString = "<Your Connection String Here>";
        // More Stuff.....
    }
}
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • I like this answer too. It makes the most sense to me as a novice with EF; using proper language to describe what's happening. The other's explanations are not clear as to what exactly is naming the DB. – eaglei22 Jan 19 '18 at 13:45
1

If you point your connection-string at an existing database then EF “code first” will not try and create one automatically.

EF “code first” uses a convention where context classes by default look for a connection-string that has the same name as the context class.

Using ef code first with an existing database

Daniel Little
  • 16,975
  • 12
  • 69
  • 93
0

For reference, here is how to do it in code using VB.NET:

Public Class DatabaseContext : Inherits DbContext

Public Property Users As DbSet(Of User)

Public Sub New()
    MyBase.New("NewFileName.sdf")
End Sub

End Class

M Granja
  • 845
  • 8
  • 26
-1

You can specify the name of the connection string the context constructor:

public YourDbContext()
    : base("Name=YourDbContext")
{
}
Mike
  • 419
  • 4
  • 6