Get database name inside Up migration method

How to get the name of the database that the migration works with from a method Up(or Down) of a class that extendsMigration

public class BaseMigration : Migration
{
    public override void Up()
    {
        var databaseName = HOW_DO_I_GET_THIS?
    }
}
+4
source share
1 answer

You can get the database name from the connection string if it is specified there. For example, this will work if you use a SQL server:

public class BaseMigration : Migration
{
    public override void Up()
    {
        System.Data.SqlClient.SqlConnectionStringBuilder builder =
            new System.Data.SqlClient.SqlConnectionStringBuilder(ConnectionString);
        var databaseName = builder.InitialCatalog;
    }
}

Otherwise, use one of the available : System.Data.Common.DbConnectionStringBuilder

System.Data.EntityClient.EntityConnectionStringBuilder
System.Data.Odbc.OdbcConnectionStringBuilder
System.Data.OleDb.OleDbConnectionStringBuilder
System.Data.OracleClient.OracleConnectionStringBuilder

See fooobar.com/questions/30023 / ... for more information .

0
source

Source: https://habr.com/ru/post/1670938/


All Articles