62

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?

1
  • 1
    You can name your database by chaining your context's constructor like this public MyContext() : base("NameForTheDB") { } Commented Feb 26, 2016 at 13:39

8 Answers 8

65

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

Sign up to request clarification or add additional context in comments.

1 Comment

Here is a link that explains all the different options: msdn.microsoft.com/en-us/data/jj592674
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)
    {
    }

}

Comments

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/

Comments

8

Alternatively you can set the name in your DbContext constructor.

2 Comments

@BrunoSalvino public DatabaseContext(): base("DatabaseName") {} will do the trick. You don't have to have the connection string defined.
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.....
    }
}

1 Comment

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.
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

Comments

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

Comments

-1

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

public YourDbContext()
    : base("Name=YourDbContext")
{
}

2 Comments

That doesn't specify a database name.
You're right Gert. I forgot to change the connection string reference "Name= ..." to "leDbNomme"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.