我有这个型号:
public class LkUpTitleModel
{
[Key]
public int TitleID { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = " * Has been left empty")]
[DisplayName("Title")]
[Remote("LkUpTitle", "Treeview", AdditionalFields = "TitleID", HttpMethod = "POST", ErrorMessage = " * Already exists")]
public string Title { get; set; }
}
绑定到此db上下文
namespace MyWork.DAL
{
public class DataBaseContext : DbContext
{
public DataBaseContext() : base("name = constring")
{}
public DbSet<LkUpGenderModel> vwLkUpGender { get; set; }
public DbSet<LkUpTitleModel> vwLkUpTitle { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
}
在控制器中:
public DataBaseContext db = new DataBaseContext();
这是远程验证功能中的类
[HttpPost]
public JsonResult LkUpTitle(string Title, int? TitleID)
{
if (TitleID == null)
{
TitleID = 0;
}
var title = (from g in db.vwLkUpTitle where g.TitleID != TitleID select g).ToList();
return Json(!title.Any(x => x.Title.ToUpper() == Title.ToUpper()));
}
当它运行并尝试验证时,我得到以下错误。不确定为什么要寻找dbo.LkUpTitleModel而不是dbo.vwLkUpTitle
&#39; /&#39;中的服务器错误应用
无效的对象名称&#39; dbo.LkUpTitleModel&#39;。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:无效的对象名称&#39; dbo.LkUpTitleModel&#39;。
来源错误:
第135行:} 第136行: 第137行:var title =(来自db.vwLkUpTitle中的g,其中g.TitleID!= TitleID select g).ToList(); 第138行: 第139行:返回Json(!title.Any(x =&gt; x.Title.ToUpper()== Title.ToUpper()));
源文件:&gt; C:\ Users \ RLewis \ Source \ Workspaces \ Workspace \ Orwell \ OrwellFrontEnd \ Controllers \ TreeviewController.cs Line:137
堆栈追踪:
[SqlException(0x80131904):无效的对象名称&#39; dbo.LkUpTitleModel&#39;。] System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action
1 wrapCloseInAction) +2508250 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)+81 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)+606 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&amp; dataReady)+4157 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+58 System.Data.SqlClient.SqlDataReader.get_MetaData()+ 89 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)+369 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task&amp; task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest)+2047 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +394 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext
1 c)+9 System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func
3操作,TInterceptionContext interceptionContext,Action3 executing, Action
3执行)+72 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)+355 System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)+167 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)+12 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+37[EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。] System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+112 System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context,ObjectParameterCollection parameterValues)+738 System.Data.Entity.Core.Objects。&lt;&gt; c__DisplayClass7.b__6()+97 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func
1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288 System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +155 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1次操作)+189 System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption)+280 System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +11 System.Data.Entity.Internal.LazyEnumerator
1.MoveNext()+45 System.Collections.Generic.List1..ctor(IEnumerable
1个集合)+381 C:\ Users \ RLewis \ Source \ Workspaces \ Workspace \ Orwell \ OrwellFrontEnd \ Controllers \ TreeviewController.cs中的System.Linq.Enumerable.ToList(IEnumerable1 source) +58 OrwellFrontEnd.Controllers.TreeviewController.LkUpTitle(String Title, Nullable
1 TitleID):137 lambda_method(Closure,ControllerBase,Object [])+151 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2个参数)+27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()+50 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass46.b__3f()+225 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2b.b__1c()+26 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult asyncResult)+100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)+13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult,Controller controller)+12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult,ProcessRequestState innerState)+21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9720041 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155