How to specify database name in Code First?

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

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

+48
entity-framework-4 ef-code-first
Mar 18 2018-11-11T00:
source share
7 answers

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

+53
Mar 18
source share

How to use a different connection string name with EF

EF will use the database name in the connection string. If you want to separate the name of your connection string from EF, you need to specify the connection string for the constructor. Example:

public class DatabaseContext : DbContext { public DatabaseContext() : base(ApplicationParameters.ConnectionStringName) { } public DatabaseContext(string connectionStringName) : base(connectionStringName) { } } 
+20
Mar 07 2018-12-12T00:
source share

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/

+15
May 12 '14 at 19:20
source share

Alternatively, you can set the name in your DbContext constructor.

+7
Mar 25 2018-11-17T00:
source share

If you specify your connection string in an existing database, then EF "code first" will not try to create it automatically.

EF "code first" uses a convention in which 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

+1
Mar 18 2018-11-11T00:
source share

As already mentioned, you can declare your connection string inside the configuration file of your application with a name (for example, “YourDBName”), and then pass this to the base constructor of DbContext (I will add this in response to providing a complete answer - great answers already given to this).

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

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
Feb 02 '17 at 17:56 on
source share

For reference, here's 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 

Final class

0
Mar 26 '15 at 12:53
source share



All Articles