0

I have this model:

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; }
}

Tied into this db context

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);
        }
    }
}

In the controller:

public DataBaseContext db = new DataBaseContext(); 

This is the class in the function for the remote validation

[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()));
}

When it runs and tries the validation I get the below error. Not sure why it is looking for dbo.LkUpTitleModel and not dbo.vwLkUpTitle

Server Error in '/' Application.

Invalid object name 'dbo.LkUpTitleModel'.

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.LkUpTitleModel'.

Source Error:

Line 135: } Line 136: Line 137: var title = (from g in db.vwLkUpTitle where g.TitleID != TitleID select g).ToList(); Line 138: Line 139: return Json(!title.Any(x => x.Title.ToUpper() == Title.ToUpper()));

Source File: >C:\Users\RLewis\Source\Workspaces\Workspace\Orwell\OrwellFrontEnd\Controllers\TreeviewController.cs Line: 137

Stack Trace:

[SqlException (0x80131904): Invalid object name 'dbo.LkUpTitleModel'.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +2508250 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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& 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& 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, DbCommandInterceptionContext1 c) +9 System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func3 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) +72 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, 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: An error occurred while executing the command definition. See the inner exception for details.] 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.<>c__DisplayClass7.b__6() +97 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func1 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(Func1 operation) +189 System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) +280 System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +11 System.Data.Entity.Internal.LazyEnumerator1.MoveNext() +45 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +381 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 OrwellFrontEnd.Controllers.TreeviewController.LkUpTitle(String Title, Nullable1 TitleID) in C:\Users\RLewis\Source\Workspaces\Workspace\Orwell\OrwellFrontEnd\Controllers\TreeviewController.cs:137 lambda_method(Closure , ControllerBase , Object[] ) +151 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +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.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +225 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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& completedSynchronously) +155

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
JQuery
  • 889
  • 3
  • 13
  • 35
  • do you have a table in your database named "LkUpTitleModel" ? – dansasu11 May 18 '15 at 15:29
  • Yeah. Most likely, you need to add a new migration and update your database. – Chris Pratt May 18 '15 at 15:55
  • here is some useful links: [invalid-object-name-dbo-item][1] [invalid-object-name-dbo-userroles][2] [root-cause-of-an-invalid-object-name-dbo-etc-error][3] [1]: http://stackoverflow.com/questions/23729159/invalid-object-name-dbo-item [2]: http://stackoverflow.com/questions/18725804/invalid-object-name-dbo-userroles [3]: http://stackoverflow.com/questions/3967800/root-cause-of-an-invalid-object-name-dbo-etc-error – fbarikzehy May 18 '15 at 16:13
  • Hi, there is no table dbo.LkUpTitleModel and I don't want there to be either. Based on the code I would expect it to look for vwLkUpTitle which does exist. I can't work out why it is looking for the model name and not the table. – JQuery May 19 '15 at 06:58
  • @JQuery why should it look for `vwLkUpTitle` when you named the class `LkUpTitleModel` ? The *convention* is to match classes and tables by name. You can specify a different table with the [TableAttribtue](http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.aspx) – Panagiotis Kanavos Feb 23 '17 at 12:29

1 Answers1

0

EF is matching DBSet and tables by Model name, that means if your DbSet is type of "LkUpTitleModel" then it will be looking for table named "LkUpTitleModel". If you want it to work properly change DbSet type from "LkUpTitleModel" to "vwLkUpTitle".

de0
  • 1
  • 1