Domanda

I created a DBContext Initializer:

Public Class DropCreateInitializer(Of T As DbContext)
    Inherits DropCreateDatabaseIfModelChanges(Of T)
    Protected Overrides Sub Seed(context As T)
        context.Database.ExecuteSqlCommand("CREATE INDEX IX_EXPLAN ON DBO.EXPLANS (PROGNAME, BIND_TIME, ACCESSNAME)")
    End Sub
End Class

What I dont understand is how to control the SQL server the new context will create a database on. It gets created on localhost/SQLEXPRESS.

È stato utile?

Soluzione

The DbContext class optionally takes a connection string as a constructor parameter, which means you can programatically build up a connection string using the System.Data.SqlClient.SqlConnectionStringBuilder class.

Here is an example, in C# (I know your sample is in vb.net, but the translation should be simple enough, and this should illustrate the approach):

public class MyContext : DbContext
{
    public MyContext(string serverName, string databaseName)
        : base(GetConnectionString(serverName, databaseName))
    {

    }

    private static string GetConnectionString(string serverName, string databaseName)
    {
        var connectionBuilder = new SqlConnectionStringBuilder
        {
            DataSource = serverName,
            InitialCatalog = databaseName,
            IntegratedSecurity = true
        };

        var connectionString = connectionBuilder.ToString();
        return connectionString;
    }
}

Altri suggerimenti

You control it in connection string passed to your context or defined in your configuration file. If you don't use any EF will use the default one which uses local SQL Express.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top