Phe*_*eap 9 sql-server-express localdb entity-framework-6 visual-studio-2013 asp.net-mvc-5
我一直在使用MVC 5来学习本教程Entity Framework 6 Code First入门.
当我到达我需要运行程序的部分并单击Student以便程序将创建数据库时,我收到以下错误.

用户代码未处理System.Data.DataException HResult = -2146233087消息=初始化数据库时发生异常.有关详细信息,请参阅InnerException.Source = EntityFramework StackTrace:System.Data.Entity.InternalContext.PerformInitializationAction(Action action)at System.Data.Entity.InternalContext.PerformDatabaseInitialization()at System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)处于System.Data.Entity.Inntial
1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action上的System.Data.Entity.InternalContext.Initialize()处的System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()处的System.Data.Entity.Internal.RetryAction 1 action)位于System.Data.Entity.Internal.Linq.InternalSet的.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType).位于ContosoUniversityFollow.Controllers.StudentController的1.Initialize() at System.Data.Entity.Internal.Linq.InternalSetSystem.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)的1.GetEnumerator() .Index()位于c:\ Users\Office\Documents\Visual Studio 2013\Projects\ContosoUniversityFollow\ContosoUniversityFollow\Controllers\StudentController.cs:第21行,位于System.Web.Mvc的lambda_method(Closure,ControllerBase,Object []). ActionMethodDisp System.Web上System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()的System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2参数)的atcher.Execute(ControllerBase控制器,Object []参数)System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult中的.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult,ActionInvocation innerInvokeState)2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()在System.Web.Mvc.Async.AsyncResultWrapper.End [TResult](IAsyncResult asyncResult)在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的asyncResult)在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3c()在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters,对象标签).< > c__DisplayClass45.b__3e()InnerException:System.Data.Entity.Core.EntityException HResult = -2146233087 Message =基础提供程序在Open上失败.Source = EntityFramework StackTrace:位于System.Data.Entity上的System.Data.Entity.Core.EntityClient.EntityConnection.Open()处于System.Data.Entity.Cnt.Entity.在System.Lazy 1的1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuerySystem.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery1..GetEnumerator> b__0())中的ExecuteInTransaction [T](Func 1.<> c__DisplayClassb.b__9()1.CreateValue() at System.Lazy.LazyInitValue()在System.Lazy1.get_Value() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext()在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable1序列)的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](IQueryable1来源)System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func2 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.DropCreateDatabaseIfModelChanges1.InitializeDatabase(TContext context)在System.Data.Entity.Internal.InternalContext.<> c__DisplayClasse1.<CreateInitializationAction>b__d() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) InnerException: System.Data.SqlClient.SqlException HResult=-2146232060 Message=Cannot open database "ContosoUniversity1" requested by the login. The login failed. Login failed for user 'Office-PC\Office'. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=11 LineNumber=65536 Number=4060 Procedure="" Server=(LocalDb)\v11.0 State=1 StackTrace: 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, TaskCompletionSource1重试,DbConnectionOptions userOptions,DbConnectionInternal&connection)在System. Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskC 在System.Data.Entity的System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1操作)的1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSourceSystem.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)处的ompletionSource 1重试,DbConnectionOptions userOptions1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func. System.Data.Entity.Core.EntityClient.EntityConnection.Open()中的SqlServer.DefaultSqlExecutionStrategy.Execute(Action操作)InnerException:
我很难尝试解决问题.我遵循一切,甚至重做它只是为了确保我做对了.我认为我不会错过任何步骤.但是有些不对劲.如果我运行整个示例代码,一切正常.
任何帮助表示赞赏.
谢谢,
Pheap
编辑:
对于导致这样的错误的简单事情来说真的很令人沮丧.@mmeasor,我刚看到那个错误.它无法连接到数据库.所以这导致我检查连接.
发生的事情是,起初我使用了这个连接字符串:

因此,EntityFramework在此处创建了一个数据库文件ContosoUniversity1.mdf,如C:\ Users\Office\ContosoUniversity1.mdf.但后来我希望它创建另一个并使用它而这次我希望它在程序App_Data \文件夹中.
所以我将web.config文件修改为:

我想如果我这样做,EF将在\ App_Data \文件夹中为我创建一个新数据库并链接到它而不是C:\ Users\Office中的旧数据库.
我错了!那时我开始收到上面发布的错误.我找不到导致问题的原因.
使用第二个web.config,我尝试将数据库文件复制到App_Data文件夹,但仍然无效.
现在我使用第一个连接字符串,它工作正常.
所以现在的问题是:如何使程序链接到\ App_Data \文件夹中的数据库文件?
谢谢,
Pheap
我在尝试将 Conosto 大学教程数据库的位置从 User 文件夹更改为 App_Data 文件夹时遇到了同样的问题。约翰·洛克(John Locke)让我意识到这可能与必须更改数据库名称有关。
稍微更改 web.config 文件中命名的数据库的名称(这工作不一致,但工作始终不一致。如果我必须再次测试这个......我确信我会在某个时候......我会远离从重用名称 - 即不要在两个名称首选项之间来回切换(例如database1和database2回到database1 - 我会使用完全新颖的名称,例如databse7...database8...database9 - 从以前使用的名称中获取名称)
我也很幸运,一旦使用我不想要的名称创建了数据库,将数据库名称更改为我想要的名称并在 web.config 中匹配该名称(即我能够创建一个“database3.mdf” “ 文件成功,然后在操作系统中将其更改为我想要的内容 - “database1.mdf” 并将 web.config 文件与 database1 匹配,并且在第一次尝试时就连接了)
| 归档时间: |
|
| 查看次数: |
32978 次 |
| 最近记录: |