How to specify database name in Code First?
Asked Answered
M

8

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?

Muscatel answered 18/3, 2011 at 0:52 Comment(1)
You can name your database by chaining your context's constructor like this public MyContext() : base("NameForTheDB") { }Dita
A
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.

Acquiesce answered 18/3, 2011 at 0:59 Comment(1)
Here is a link that explains all the different options: msdn.microsoft.com/en-us/data/jj592674Weighted
Y
24

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)
    {
    }

}
Yorkshire answered 7/3, 2012 at 20:48 Comment(0)
J
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/

Janik answered 12/5, 2014 at 19:20 Comment(0)
S
8

Alternatively you can set the name in your DbContext constructor.

Selffertilization answered 25/3, 2011 at 17:42 Comment(2)
Check this out: Change Database Name Created by Code FirstHappen
@BrunoSalvino public DatabaseContext(): base("DatabaseName") {} will do the trick. You don't have to have the connection string defined.Squiffy
K
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.....
    }
}
Kristelkristen answered 2/2, 2017 at 17:56 Comment(1)
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.Knavery
S
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

Salcido answered 18/3, 2011 at 0:56 Comment(0)
H
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

Hardee answered 26/3, 2015 at 12:53 Comment(0)
P
-1

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

public YourDbContext()
    : base("Name=YourDbContext")
{
}
Principate answered 17/1, 2022 at 23:8 Comment(2)
That doesn't specify a database name.Ditchwater
You're right Gert. I forgot to change the connection string reference "Name= ..." to "leDbNomme"Principate

© 2022 - 2024 — McMap. All rights reserved.