结合使用带有AccessToken的SqlConnection和Entity Framework 6迁移

时间:2018-06-21 10:12:07

标签: entity-framework entity-framework-6 azure-sql-database ef-migrations sqlconnection

我正在尝试将EntityFramework 6迁移与Azure数据库一起使用。我正在使用带有AccessToken的SqlConnection来连接到Azure数据库并迁移。

public class MasterDbContext : DbContext
{
    //...

    public MasterDbContext(SqlConnection connection)
        : base(connection,true)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MasterDbContext, Migrations.Configuration>(true));
    }

    //...


}            

public async Task SeedMasterDatabaseAsync(string connectionString)
{
    SqlConnection conn = new SqlConnection(connectionString)
    {
        AccessToken = "xyc"
    };

    using (var dbcontext = new MasterDbContext(conn))
    {
        dbcontext.Database.Initialize(false);
    }
}

我收到以下例外情况:

Unhandled Exception: System.InvalidOperationException: This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection. ---> System.Data.SqlClient.SqlException: Login failed for user ''.

该异常消息表示DBMigrator没有使用SQL连接,而是尝试从连接字符串创建新的连接。

这是我的连接字符串:

<add name="MasterDbContext" connectionString="Server=foo;Persist Security Info=True;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />

问题是,EF6.3迁移与Azure数据库一起支持AccessToken吗?还是这是主数据库上的权限问题?如果必须使用AccessToken,有什么解决方法?

0 个答案:

没有答案