当POCO文件位于单独的项目中时,将EF指向数据库

时间:2015-05-01 00:48:50

标签: c# asp.net-mvc entity-framework azure-sql-database

我有一个单独的ASP.Net解决方案,里面有3个项目。数据访问层(DAL),包含代码,用于我的数据库,API项目和UI项目的第一个CS文件。我已经设置了API的web.config文件以指向Azure中的测试数据库,我已将实体框架安装到API和DAL中。

当我使用UPDATE-DATABASE从程序包管理器控制台运行我的第一次迁移时,我收到错误No migrations configuration type was found in the assembly API所以我运行Enable-Migrations但得到错误No context type was found in the assembly API 。然后,我将默认项目设置为DAL并运行Update-Database,并在网络连接错误时出错。我可以进入MSSQL Studio并使用我的连接字符串中的相同连接数据并登录就好了。那么我做错了什么?我怎样才能有一个单独的DAL项目,但仍然可以正常工作?

这是我得到的完整错误

  

PM&GT; UPDATE-DATABASE指定'-Verbose'标志以查看SQL   语句被应用于目标数据库。   System.Data.SqlClient.SqlException(0x80131904):与网络相关或   建立连接时发生特定于实例的错误   SQL Server。服务器未找到或无法访问。校验   实例名称正确且SQL Server配置为   允许远程连接。 (提供者:SQL网络接口,错误:26    - 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException)中找错“查找指定的服务器/实例”   exception,Boolean breakConnection,Action 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource 1 retry,DbConnectionOptions   userOptions,DbConnectionInternal&amp;连接)   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(的DbConnection   owningConnection,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1次重试,DbConnectionOptions userOptions)at at   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(的DbConnection   outerConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1   重试)   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch [t目标,TInterceptionContext](t目标   目标,行动2 operation, TInterceptionContext interceptionContext, Action 3执行,行动3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1   操作)   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(动作   操作)   System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(的DbConnection   sqlConnection,Action 1 act) at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action 1 act)at   System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable 1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable 1 commandTimeout,StoreItemCollection   storeItemCollection)at   System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(的DbConnection   connection,Nullable`1 commandTimeout,StoreItemCollection   storeItemCollection)at   System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()at   System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(的DbConnection   连接)   System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作   mustSucceedToKeepDatabase)at   System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(动作   mustSucceedToKeepDatabase)at   System.Data.Entity.Migrations.DbMigrator.Update(字符串   targetMigration)   System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串   targetMigration)   System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()   在System.AppDomain.DoCallBack(CrossAppDomainDelegate   callBackDelegate)at   System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
  在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner)   跑者)   System.Data.Entity.Migrations.Design.ToolingFacade.Update(字符串   targetMigration,Boolean force)at   System.Data.Entity.Migrations.UpdateDatabaseCommand&LT;&GT; c__DisplayClass2&LT; .ctor&GT; b__0()   在   System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(动作   命令)ClientConnectionId:00000000-0000-0000-0000-000000000000错误   编号:-1,状态:0,类:20与网络相关或特定于实例   建立与SQL Server的连接时发生错误。该   服务器未找到或无法访问。验证该实例   名称是正确的,并且SQL Server配置为允许远程   连接。 (提供者:SQL网络接口,错误:26 - 错误   找到指定的服务器/实例)

1 个答案:

答案 0 :(得分:1)

另一个选择是通过添加

强制EF包管理器使用正确的连接字符串和提供者
Update-Database -ConnectionString "Data Source=(LocalDb)\v11...." -ConnectionProviderName  "System.Data.SqlClient"