gpt4 book ai didi

visual-studio-2013 - EntityFramework.dll 中发生 System.Data.DataException 错误

转载 作者:行者123 更新时间:2023-12-02 09:37:51 25 4
gpt4 key购买 nike

我一直在关注这个教程Getting Started with Entity Framework 6 Code First using MVC 5 .

当我到达需要运行程序的部分并单击“学生”以便程序创建数据库时,出现以下错误。

enter image description here

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.b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction1.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.InternalSet1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet
1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at ContosoUniversityFollow.Controllers.StudentController.Index() in c:\Users\Office\Documents\Visual Studio 2013\Projects\ContosoUniversityFollow\ContosoUniversityFollow\Controllers\StudentController.cs:line 21 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() at System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.b__3e() 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](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClassb.b__9() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator>b__0() at System.Lazy1.CreateValue()
at System.Lazy
1.LazyInitValue() at System.Lazy1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator
1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 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](IEnumerable1 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(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.DropCreateDatabaseIfModelChanges
1.InitializeDatabase(TContext context) at 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, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource
1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 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
1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.Core.EntityClient.EntityConnection.Open() InnerException:

我很难解决这个问题。我遵循一切,甚至重做以确保我做对了。我不认为我错过了任何步骤。但有一点不对劲。如果我运行整个示例代码,一切都会正常。

感谢任何帮助。

谢谢

菲普

编辑:

一个简单的事情导致这样的错误真是令人沮丧。@mmeasor,我也刚刚看到了这个错误。它无法连接到数据库。这让我检查连接。

首先我使用了这个连接字符串: enter image description here

因此EntityFramework在这里创建了一个数据库文件ContosoUniversity1.mdf,例如C:\Users\Office\ContosoUniversity1.mdf。但后来我希望它创建另一个并使用它,这次我希望它位于程序 App_Data\文件夹中。

所以我将 web.config 文件修改为: enter image description here

我想,如果我这样做,EF 将在\App_Data\文件夹中为我创建一个新数据库,并链接到该数据库,而不是 C:\Users\Office 中的旧数据库。

我错了!那是我开始收到上面发布的错误的时候。而且我找不到导致问题的原因。

对于第二个 web.config,我尝试将数据库文件复制到 App_Data 文件夹,但这仍然不起作用。

现在我使用第一个连接字符串,它工作正常。

现在的问题是:如何让程序链接到\App_Data\文件夹中的数据库文件?

谢谢

菲普

最佳答案

我在尝试将 Conosto 大学教程数据库的位置从 User 文件夹更改为 App_Data 文件夹时遇到了同样的问题。 John Locke 让我意识到这可能与必须更改数据库名称有关。

稍微更改 web.config 文件中命名的数据库的名称(这会不一致,但确实会不一致地工作。如果我必须再次测试这个......我确信我会在某个时候......我将远离重用名称 - 即不要在两个名称首选项之间来回切换(例如,database1和database2回到database1 - 我会使用完全新颖的名称,例如databse7...database8...database9 - 从以前的名称中获取名称使用的名称)

我也很幸运,一旦使用我不想要的名称创建了数据库,将数据库名称更改为我想要的名称并在 web.config 中匹配该名称(即我能够创建一个“database3.mdf” 文件成功,然后在操作系统中将其更改为我想要的 - “database1.mdf” 并将 web.config 文件与 database1 匹配,并且在第一次尝试时就连接了)

关于visual-studio-2013 - EntityFramework.dll 中发生 System.Data.DataException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com