无法附加文件作为数据库/基础提供失败的开放 [英] Cannot attach the file as database / The underlying provider failed on Open

查看:415
本文介绍了无法附加文件作为数据库/基础提供失败的开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架的应用MVC3。我没有在一年内使用这个应用程序。最近,我打开它在新PC上,并试图在调试模式下运行,并在调试击中以下异常/堆栈跟踪。主要的例外似乎是无法将文件附加数据库和基础提供开放式失败。我搜索了关于堆栈溢出,我发现似乎并不符合这种情形的答案这些错误。例如,答案为无法将文件附加数据库之一表明,一个旧版本的分贝可能躺在附近的本地数据库,但我没有看到任何数据库在本地数据库中,当我浏览到的LocalDB在Visual Studio中。

I have an MVC3 application using the Entity Framework. I haven't used this application in about a year. Recently, I opened it up on a new PC and tried to run in debug mode and hit the following exception/stack trace in debug. The main exceptions seem to be "Cannot attach the file as database" and "The underlying provider failed on Open". I searched for these errors on stack overflow and the answers I found don't seem to match this situation. For example, one of the answers for "Cannot attach the file as database" suggests that an old version of the db might be lying around on the local db, but I don't see any databases in the local database when I browse to localdb in visual studio.

我要寻找有关下一个解决此问题的调查什么指导。有什么建议?

I am looking for guidance about what to investigate next to troubleshoot this issue. Any suggestions?

实际code其中失败是:

The actual code where it fails is:

            var currentHunt = (from ph in this.repo.GetAllPuzzleHunts()
                           orderby ph.PuzzleHuntId descending
                           select ph).FirstOrDefault();

这是第一次在Web应用程序试图从通过实体框架的数据库获取数据。

This is the first time the web app tries to get data from the db via the Entity Framework.

FWIW该机拥有实体框架6.0和我使用Visual Studio 2012。

FWIW this machine has Entity Framework 6.0, and I'm using Visual Studio 2012.

异常/堆栈跟踪如下:

System.Data.DataException was unhandled by user code
  HResult=-2146233087
  Message=An exception occurred while initializing the database. See the InnerException for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
       at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at GutolMvcApp.Controllers.HomeController.Index() in c:\Users\jordanat\Documents\Visual Studio 2012\Projects\Gutol\Gutol\GutolMvcApp\Controllers\HomeController.cs:line 25
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: System.Data.Entity.Core.EntityException
       HResult=-2146233087
       Message=The underlying provider failed on Open.
       Source=EntityFramework
       StackTrace:
            at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
            at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
            at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
            at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
            at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
            at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
            at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
            at System.Lazy`1.CreateValue()
            at System.Lazy`1.LazyInitValue()
            at System.Lazy`1.get_Value()
            at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
            at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
            at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
            at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
            at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
            at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
            at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
            at System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func`2 createContext)
            at System.Data.Entity.Internal.InternalContext.QueryForModelHash()
            at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
            at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
            at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
            at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
            at System.Data.Entity.Internal.InternalContext.<>c__DisplayClasse`1.<CreateInitializationAction>b__d()
            at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       InnerException: System.Data.SqlClient.SqlException
            HResult=-2146232060
            Message=Cannot attach the file 'C:\Users\...\Visual Studio 2012\Projects\Gutol\Gutol\GutolMvcApp\App_Data\PuzzleCollateral.mdf' as database 'PuzzleCollateral'.

感谢您的帮助!

推荐答案

检查您的连接字符串。如果你是一个新的计算机上可能需要有所改变他们或在防火墙上打开一个端口。

Check your connection strings. If you're on a new computer you may need to change them somewhat or open a port in the firewall.

一个简单的方法来获取正确的连接字符串连接到数据库服务器,并查看属性。

One simple way to get the proper connection string is to connect to the DB Server and view the properties.


  1. 开启的SQL Server对象资源管理器(查看> SQL Server的对象资源管理器)

  2. 连接到数据库服务器(点击连接到服务器,进入(的LocalDB)\\ 11.0)

  3. 右键单击相应的数据库,并选择属性

  4. 查看连接字符串

这篇关于无法附加文件作为数据库/基础提供失败的开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆