更新 - 数据库连接字符串问题

时间:2014-06-09 12:29:41

标签: entity-framework-5 ef-migrations

我们有一个代码优先实体框架项目和一个关联的app.config文件。

连接字符串指向SQL Azure实例。当我们将应用程序部署到Cloud Service并要求它自动创建数据库时,它会这样做,但似乎没有运行配置代码,因为我们看到空表。

当我运行" update-database"从Azure托管的Dev VM中,我收到以下错误:

  

从中获取提供者信息时发生错误   数据库。这可能是由实体框架使用不正确引起的   连接字符串。检查内部异常以获取详细信息并确保   连接字符串是正确的。

进一步了解

System.Data.Entity.Core.ProviderIncompatibleException: An error occurred while getting provider information from the database. 
This can be caused by Entity Framework using an incorrect connection string. 
Check the inner exceptions for details and ensure that the connection string is correct. 
---> System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. 
---> System.MissingMethodException: Method not found: 'System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher

然而,它与我们在已部署的实例中用于创建数据库的连接字符串相同。

有关更新数据库失败原因的任何建议吗?

2 个答案:

答案 0 :(得分:0)

Azure SQL数据库使用SQL登录,因此用户标识和密码位于连接字符串中。如果您没有Persist Security Info = True,那么凭证会在EF中丢失。我认为这在EF 6.0中已被打破。

答案 1 :(得分:0)

我想提供帮助,所以这里是我们使用的解决方法。我们在DbContext上实现Dispose并在处理上下文后恢复连接字符串,以便以后不会阻塞迁移:

    protected override void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            _disposed = true;
            var connection = Database.Connection;
            var connectionString = Database.Connection.ConnectionString;
            base.Dispose(disposing);
            connection.ConnectionString = connectionString;
        }
    }