我收到此错误:[SqlException(0x80131904):无效的对象名称' dbo.UserRoles'。],但我无法捕获错误的位置。我可以从其他表中检索其他数据,但不是。
条件是:
web.config中的连接字符串:
<add name="myStoreConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename='|DataDirectory|\myStore.mdf';Integrated Security=True" providerName="System.Data.SqlClient" />
上下文:
public class AdminMyStoreConnection : DbContext
{
public DbSet<UserRole> UserRoles { get; set; }
}
用于创建数据库的sql脚本:
CREATE TABLE [dbo].[UserRoles] (
[RoleId] INT IDENTITY (1, 1) NOT NULL,
[RoleName] NVARCHAR (256) NOT NULL,
PRIMARY KEY CLUSTERED ([RoleId] ASC),
UNIQUE NONCLUSTERED ([RoleName] ASC)
);
模型:
[Table("UserRoles")]
public class UserRole
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RoleId { get; set; }
public string RoleName { get; set; }
}
带索引视图的控制器:
private UsersContext db = new UsersContext();
public ActionResult Index()
{
return View(db.Customers.ToList());
}
视图:
@using myStore.Helpers
@model IEnumerable<myStore.Areas.Administrator.Models.UserRole>
@{
ViewBag.Title = "Roles management";
}
<h2>Roles management</h2>
<div class="admin-container">
<div class="bcontrol">
<a href="@Url.Action("Create", "Roles")" class="btn btn-large btn-primary"><i class="icon-plus-sign"></i> Create New Entry</a>
</div>
@{
var grid = new WebGrid(Model, defaultSort: "RoleId", canSort:true, canPage: true, rowsPerPage:20);
}
@grid.GetHtml(
tableStyle: "tadmin",
alternatingRowStyle: "alt",
selectedRowStyle: "selected-row",
columns:
grid.Columns(
grid.Column("RoleId",
header: "ID " + Html.SortDirection(ref grid, "RoleId"),
style: "tadmin-id"),
grid.Column("RoleName",
header: "Role Name " + Html.SortDirection(ref grid, "RoleName"),
format: @<text><a href="@Url.Action("Details", "Roles", new { id = item.RoleId })" >@item.RoleName</a></text>),
grid.Column("Control", style: "tadmin-control", canSort: false,
format:
@<text><a href="@Url.Action("Edit", "Roles", new { id = item.RoleId })" class="btn btn-large btn-primary"><i class="icon-edit"></i> Edit</a>
<a href="@Url.Action("Details", "Roles", new { id = item.RoleId })" class="btn btn-large btn-primary"><i class="icon-list"></i> Details</a>
<a href="@Url.Action("Delete", "Roles", new { id = item.RoleId })" class="btn btn-large btn-primary"><i class="icon-minus-sign"></i> Delete</a></text>)
)
)</div>
和错误消息:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.UserRoles'.
Source Error:
Line 19: public ActionResult Index()
Line 20: {
Line 21: return View(db.UserRoles.ToList());
Line 22: }
Line 23:
和堆栈跟踪:
[SqlException(0x80131904):无效的对象名称&#39; dbo.UserRoles&#39;。]
System.Data.SqlClient.SqlConnection.OnError(SqlException异常, Boolean breakConnection,Action`1 wrapCloseInAction)+1753986
System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection,Action`1 wrapCloseInAction) +5296058 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+558
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔&amp; dataReady)+1682
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+59
System.Data.SqlClient.SqlDataReader.get_MetaData()+90
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)+365
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32超时,任务&amp; task,Boolean asyncWrite)+1379
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource`1完成,Int32超时,任务&amp;任务, Boolean asyncWrite)+175
System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)+53
System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior 行为,字符串方法)+134
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为)+41
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand) entityCommand,CommandBehavior行为)+437[EntityCommandExecutionException:执行时发生错误 命令定义。有关详细信息,请参阅内部异常。]
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior行为)+507
System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext的 context,ObjectParameterCollection parameterValues)+730
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)+131
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() +36 System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()+72
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()+23
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() +40 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)+369 System.Linq.Enumerable.ToList(IEnumerable`1 来源)+58
myStore.Areas.Administrator.Controllers.RolesController.Index()in d:\ .projects火车\的MyStore \的MyStore \区\管理\ \控制器RolesController.cs:21 lambda_method(Closure,ControllerBase,Object [])+62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数)+14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)+211
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2 参数)+27
System.Web.Mvc.Async&LT;&GT; c__DisplayClass42.b__41() +28 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass8`1.b__7(IAsyncResult _)+10 System.Web.Mvc.Async.WrappedAsyncResult`1.End()+57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult) asyncResult)+48
System.Web.Mvc.Async&LT;&GT; c__DisplayClass39.b__33() +57 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass4f.b__49() +223 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass37.b__36(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult) asyncResult)+48
System.Web.Mvc.Async&LT;&GT; c__DisplayClass2a.b__20() +24 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass25.b__22(IAsyncResult) asyncResult)+102 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult) asyncResult)+43
System.Web.Mvc&LT;&GT; c__DisplayClass1d.b__18(IAsyncResult的 asyncResult)+14
System.Web.Mvc.Async&LT;&GT; c__DisplayClass4.b__3(IAsyncResult的 ar)+23 System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+57 System.Web.Mvc.Async&LT;&GT; c__DisplayClass4.b__3(IAsyncResult的 ar)+23 System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult的 asyncResult)+10
System.Web.Mvc&LT;&GT; c__DisplayClass8.b__3(IAsyncResult的 asyncResult)+25
System.Web.Mvc.Async&LT;&GT; c__DisplayClass4.b__3(IAsyncResult的 ar)+23 System.Web.Mvc.Async.WrappedAsyncResult`1.End()+62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult) 结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9628700 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:5)
遗憾的是问题非常简单 - 因为我有2个区域 - 主要和管理员,我分别有2种类型的数据连接,我在webconfig中只有1个连接字符串。在我为管理上下文添加第二行并且所有工作之后 谢谢你的关注) 所以问题在webconfig中不完整(配置错误)
答案 1 :(得分:1)
答案 2 :(得分:-1)