【问题标题】:Update-Database connection string issue更新数据库连接字符串问题
【发布时间】:2014-06-09 12:29:40
【问题描述】:

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

连接字符串指向一个 SQL Azure 实例。当我们将应用程序部署到云服务中并要求它自动创建数据库时,它会这样做,但由于我们看到的是空表,因此似乎没有运行配置代码。

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

从 数据库。这可能是由于实体框架使用了不正确的 连接字符串。检查内部异常以获取详细信息并确保 连接字符串是正确的。

详细介绍

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

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

关于 update-database 为何失败的任何建议?

【问题讨论】:

  • 你是如何运行它的?有几种方法可以为迁移命令提供连接字符串

标签: entity-framework-5 entity-framework-migrations


【解决方案1】:

Azure SQL 数据库使用 SQL 登录,因此用户 ID 和密码位于连接字符串中。如果您没有 Persist Security Info=True,则凭据会在 EF 中丢失。这是我认为在 EF 6.0 中被破坏的东西。

【讨论】:

    【解决方案2】:

    我想提供帮助,所以这是我们使用的解决方法。我们在 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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      相关资源
      最近更新 更多