ASP.NET MVC教程项目

3

我在使用《Pro ASP.NET MVC Framework》这本书中的代码时遇到了一些问题。我正在第96页,对于那些可能已经完成过这本书的人来说。当我运行代码时,我得到以下结果:

System.Data.SqlClient.SqlException was unhandled by user code
  Message="Invalid object name 'Products'."
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=16
  LineNumber=1
  Number=208
  Procedure=""
  Server=".\\SQLEXPRESSADV"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
       at System.Data.SqlClient.SqlDataReader.get_MetaData()
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
       at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
       at System.Data.Common.DbCommand.ExecuteReader()
       at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
       at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
       at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
       at System.Data.Linq.Table`1.GetEnumerator()
       at System.Data.Linq.Table`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at WebUI.Controllers.ProductsController.List() in C:\Documents and Settings\adamsb1\My Documents\Visual Studio 2008\Projects\StoreSolution\WebUI\Controllers\ProductsController.cs:line 28
       at lambda_method(ExecutionScope , 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__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

我所有的代码都能成功构建,没有出现任何错误,并且与书上的代码看起来完全一样。但是,我无法让它正常工作。我是一个老派的asp程序员,所以这对我来说是全新的。

2个回答

4
错误出现在数据库中。可能是你提供的连接字符串指向错误的数据库,或者你正在使用的数据库缺少了预期存在的“Products”表。
由于看起来你正在使用SQLExpress数据库,我猜测你还没有设置教程数据库 - 程序正在尝试从你已经在机器上设置的任何SQLExpress数据库中读取。

0
问题解决了!
David,你给了我正确的方向...谢谢!
我在连接字符串上进行了以下更改:

OLD: Server=.\SQLEXPRESSADV.SportsStore;Trusted_Connection=yes;"

针对下面的内容,现在它可以工作了。

新的: Data Source=.\SQLEXPRESSADV;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接