C# 连接字符串错误:";初始化数据库时发生异常";
我使用的是visual studio 2010 mvc3,模型优先的方法,我的连接字符串不断中断这是它带来的错误: 初始化数据库时发生异常。有关详细信息,请参见InnerException 下面是它所在的代码:C# 连接字符串错误:";初始化数据库时发生异常";,c#,.net,entity-framework,exception-handling,connection-string,C#,.net,Entity Framework,Exception Handling,Connection String,我使用的是visual studio 2010 mvc3,模型优先的方法,我的连接字符串不断中断这是它带来的错误: 初始化数据库时发生异常。有关详细信息,请参见InnerException 下面是它所在的代码: public IEnumerable<Show> GetShows() { return context.Shows.ToList(); } public IEnumerable GetShows() { 返回context.Shows.ToList(); } 以
public IEnumerable<Show> GetShows()
{
return context.Shows.ToList();
}
public IEnumerable GetShows()
{
返回context.Shows.ToList();
}
以下是例外情况的详细信息:
System.Data.DataException was unhandled by user code
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.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TheatreGroup.DAL.ShowRepository.GetShows() in E:\TheatreGroupProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\DAL\ShowRepository.cs:line 20
at TheatreGroup.Controllers.ShowsController.Index(String sortOrder, String currentFilter, String searchString, Nullable`1 page) in E:\TheatreGroupProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\Controllers\ShowsController.cs:line 46
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.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Data.EntityException
Message=The underlying provider failed on Open.
Source=System.Data.Entity
StackTrace:
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
at System.Data.EntityClient.EntityConnection.Open()
at System.Data.Objects.ObjectContext.EnsureConnection()
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](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.InternalContext.QueryForModelHash()
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.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass5.<PerformDatabaseInitialization>b__3()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
InnerException: System.Data.SqlClient.SqlException
Message=Database 'E:\TheatreGroupProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\App_Data\StudentTheatre.mdf' already exists. Choose a different database name.
Cannot attach the file 'K:\Year 3 Web Programming\AnotherWebProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\App_Data\projecttheatre.mdf' as database 'YourCatalog'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=65536
Number=1801
Procedure=""
Server=\\.\pipe\C8A38AE3-00CC-42\tsql\query
State=2
StackTrace:
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
InnerException:
用户代码未处理System.Data.DataException
Message=初始化数据库时发生异常。有关详细信息,请参见InnerException。
Source=EntityFramework
堆栈跟踪:
位于System.Data.Entity.Internal.InternalContext.PerforminizationAction(操作操作)
位于System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()处
位于System.Data.Entity.Internal.LazyInternalContext.b_uu4(InternalContext c)
在System.Data.Entity.Internal.RetryAction`1.PerformAction(输入)
位于System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 Action)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()中
位于System.Data.Entity.Internal.InternalContext.Initialize()处
位于System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型entityType)
位于System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
位于System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()处
位于System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
在E:\TheaterGroupProject\BackUpWEb28.03.2012\TheaterGroupProject\TheaterGroupProject\TheaterGroup\TheaterGroup\DAL\ShowRepository.cs:第20行中的theaterGroup.DAL.ShowRepository.GetShows()处
在E:\TheaterGroupProject\BackUpWEb28.03.2012\TheaterGroupProject\TheaterGroupProject\TheaterGroups\TheaterGroups\Controllers\ShowsController\ShowsController.cs:第46行的TheaterGroups.Controllers.Index(字符串排序器、字符串currentFilter、字符串搜索字符串、空值`1页)中
在lambda_方法中(闭包、控制器基、对象[])
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass15.b_uuu12()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)
InnerException:System.Data.EntityException
Message=基础提供程序在打开时失败。
Source=System.Data.Entity
堆栈跟踪:
位于System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(布尔openCondition、DbConnection StoreConnectionOpen、DbConnection originalConnection、字符串例外代码、字符串尝试操作、布尔和CloseStoreConnectionUnfailure)
位于System.Data.EntityClient.EntityConnection.Open()处
在System.Data.Objects.ObjectContext.EnsureReconnection()中
位于System.Data.Objects.ObjectQuery`1.GetResults(可为null`1 forMergeOption)
位于System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
位于System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1源)
在System.Data.Objects.Elink.ObjectQueryProvider.b__1[TResult](IEnumerable`1序列)
在System.Data.Objects.Elink.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1查询,表达式queryRoot)
位于System.Data.Objects.Elink.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](表达式)
在System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](表达式)处
位于System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1源)
位于System.Data.Entity.Internal.InternalContext.QueryForModelHash()处
位于System.Data.Entity.Internal.InternalContext.CompatibleWithModel(布尔ThrowifNMetadata)
位于System.Data.Entity.Database.CompatibleWithModel(布尔ThrowifNMetadata)
位于System.Data.Entity.DropCreateDatabaseIfModelChanges`1.InitializeDatabase(TContext上下文)
在System.Data.Entity.Database.c__DisplayClass2`1.b__0(DbContext c)中
在System.Data.Entity.Internal.InternalContext.c__DisplayClass5.b__3()中
位于System.Data.Entity.Internal.InternalContext.PerforminizationAction(操作操作)
InnerException:System.Data.SqlClient.SqlException
Message=数据库“E:\TheatreGroupProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\App\U Data\StudentTheatre.mdf”已存在。请选择其他数据库名称。
无法将文件“K:\Year 3 Web Programming\Another WebProject\BackUpWEb28.03.2012\TheatreGroupProject\TheatreGroup\TheatreGroup\Appu Data\projecttheatre.mdf”作为数据库“YourCatalog”附加。
Source=.Net SqlClient数据提供程序
错误代码=-2146232060
等级=16
行号=65536
数字=1801
Procedure=“”
服务器=\\。\pipe\C8A38AE3-00CC-42\tsql\query
状态=2
堆栈跟踪:
位于System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
位于System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection-owningConnection)
位于System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)