How to specify database name in Code First?

2019-01-21 16:31发布

问题:

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:

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



回答2:

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

}


回答3:

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/



回答4:

Alternatively you can set the name in your DbContext constructor.



回答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.....
    }
}


回答6:

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



回答7:

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