继续之前设定的学习目标前,先来一篇小小的外篇。按照第一篇里的配置方式配置好的工程前两天还能正常工作,昨天却突然无法通过Add-Migration命令进行数据库的升级。错误信息如下:

System.Data.Entity.Core.ProviderIncompatibleException: 从数据库中获取提供程序信息时出错。这可能是 Entity Framework 使用的连接字符串不正确导致的。有关详细信息,请查看内部异常并确保连接字符串正确。 ---> System.Data.Entity.Core.ProviderIncompatibleException: 提供程序未返回 ProviderManifestToken 字符串。 ---> System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)
   在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   在 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
   在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
   在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
   在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   在 System.Data.SqlClient.SqlConnection.Open()
   在 System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass2f.<UsingConnection>b__2d()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   在 System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act)
   在 System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)
   在 System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
   在 System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
   在 System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
   在 System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k)
   在 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   在 System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)
   在 System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
   在 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   在 System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   在 System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   在 System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   在 System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   在 System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   在 System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   在 System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   在 System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   在 System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
从数据库中获取提供程序信息时出错。这可能是 Entity Framework 使用的连接字符串不正确导致的。有关详细信息,请查看内部异常并确保连接字符串正确。
View Code

在网上查了很多相关资料,主要使用ProviderManifestToken作为搜索关键字,发现有挺多人遇到了类似问题,可能的原因如下:
1. 数据库连接字符串配错了,常见的有数据库服务器名称、数据库名拼写错误。
2. 数据库连接字符串里的用户名密码没有登录权限或者密码过期了了。
3. 数据库连接字符串配置项的name值与Context的名称不一致找不到。
4. 数据库版本是2000,也有说2005也可能有问题的。

逐一排除,首先检查数据库连接字符串,因为之前配置完是可以连接和生成数据库的,数据库连接字符串应该不会有问题。不过我还是参考了“数据库连接字符串大全”这篇文章中的连接字符串配置方式检查了两遍,结果无效,同样的配置项,用SMSS可以登录数据库,故排除。
其次检查登录权限和密码,因为刚才说过用SMSS可以登录,因此也可以排除了。
剩下的两个问题也不存在,因此配置项的问题可能性比较小了。可是数据库连不上就没法继续工作,只能硬着头皮继续排查。

先说下我的项目工程和目录结构:
项 目采用了3层架构的方式,其实对小项目有点累赘和多余,不过为了跟其他项目保持一致,还是这样设计了。项目是一个WCF服务,主工程名称为XXX,业务层 工程名称为XXX.Business,数据库层为XXX.Db,还有实体等其他的一些工程。现在唯一能想到的办法就是在项目中编写最基本的ADO.Net 代码访问数据库,检查是否是EF自身的问题。由于WCF还未完全配好,暂时不能通过顶级去执行DB层代码,因此写了一个单元测试调用DB层,在DB里写了 一个基本的读取连接字符串配置项,然后打开数据库连接的方法。第一次调试单元测试,发现配置项获取到的连接字符串为null。才想起来我的配置文件在DB 里,单元测试工程中没有EF的配置项。于是把DB里的连接字符串配置项拷贝到单元测试工程中,连接顺利打开。
就是刚才的这一点提醒了我,是不是配置文件位置有问题。最开始用DB工程生成数据库的时候,可能是把XXX.Db设为了启动项,后来配置WCF,又把XXX设为了启动项,莫非是启动项搞的鬼?变成XXX.Db为启动项,问题解决。

原来PM中的默认项目不能保证配置文件就从选择的项目中读取,小小的问题耗了大半天的时间终于解决了。
在这里向大家分享这个问题,希望能帮到你。

posted on 2014-02-24 11:17  九玄  阅读(3574)  评论(2编辑  收藏  举报