无法将文件作为数据库附加/基础提供程序在Open上失败

时间:2013-12-29 00:42:59

标签: asp.net-mvc-3 entity-framework visual-studio-debugging localdb

我有一个使用Entity Framework的MVC3应用程序。我大约一年没有使用过这个应用程序。最近,我在一台新PC上打开它并尝试在调试模式下运行并在调试中点击以下异常/堆栈跟踪。主要异常似乎是“无法将文件附加为数据库”和“基础提供程序在打开时失败”。我在堆栈溢出时搜索了这些错误,我发现的答案似乎与这种情况不符。例如,“无法将文件附加为数据库”的答案之一表明旧版本的db可能位于本地数据库上,但是当我浏览到localdb时,我在本地数据库中看不到任何数据库在视觉工作室。

我正在寻找有关解决此问题的问题的指导。有什么建议吗?

失败的实际代码是:

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

这是第一次网络应用尝试通过实体框架从数据库获取数据。

FWIW这台机器有Entity Framework 6.0,我正在使用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'.

感谢您的帮助!

2 个答案:

答案 0 :(得分:13)

以这种方式尝试:

https://stackoverflow.com/a/16339164/1390999

  

打开VisualStudio的#34;开发人员命令提示&#34;在你的   开始/程序菜单。运行以下命令:

     

sqllocaldb.exe停止v11.0

     

sqllocaldb.exe删除v11.0

之后,尝试启动MVC应用程序

答案 1 :(得分:2)

检查连接字符串。如果您使用的是新计算机,则可能需要稍微更改它们或打开防火墙中的端口。

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

  1. 打开SQL Server对象资源管理器(View&gt; SQL Server对象资源管理器)
  2. 连接到数据库服务器(单击“连接到服务器”并输入“(LocalDB)\ v11.0”)
  3. 右键单击相应的数据库,然后选择“属性”
  4. 查看连接字符串