asp.net-mvc System.Data.SqlClient.SqlException: 无效的对象名称 'dbo.Projects'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1355795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Data.SqlClient.SqlException: Invalid object name 'dbo.Projects'
提问by Joe
My MVC app is returning SqlExceptions when trying to access any table in my database.
我的 MVC 应用程序在尝试访问我的数据库中的任何表时返回 SqlExceptions。
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Projects'.
异常详细信息:System.Data.SqlClient.SqlException:对象名称“dbo.Projects”无效。
My app us linq for the data layer.
我的应用程序我们 linq 用于数据层。
If I use an old dll it works fine, (so doesn't seem to be a problem with the DB) just this latest app dll that I've uploaded.
如果我使用旧的 dll,它可以正常工作,(所以 DB 似乎没有问题)只是我上传的这个最新的应用程序 dll。
details
细节
[SqlException (0x80131904): Invalid object name 'dbo.Projects'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +83
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12 System.Data.Common.DbCommand.ExecuteReader() +12
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +975
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
System.Data.Linq.DataQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +35 System.Linq.Buffer1..ctor(IEnumerable1 source) +247 System.Linq.<GetEnumerator>d__0.MoveNext() +1081..ctor(IEnumerable
System.Linq.Buffer1 source) +2591..ctor(IEnumerable
System.Linq.<GetEnumerator>d__0.MoveNext() +108
System.Collections.Generic.List1 collection) +7665172 System.Linq.Enumerable.ToList(IEnumerable1 source) +61 Mezza_crm.Controllers.ProjectsController.GetProjectList(NameValueCollection form) in C:\mezza_crm\mezza_crm\Controllers\ProjectsController.cs:164 Mezza_crm.Controllers.ProjectsController.List() in C:\mezza_crm\mezza_crm\Controllers\ProjectsController.cs:53
lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +1782 parameters) +24
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.<>c__DisplayClassa.b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +2541 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
System.Web.Mvc.Controller.ExecuteCore() +126
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
[SqlException (0x80131904): Invalid object name 'dbo.Projects'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,Boolean breakConnection) +1950890
System.Data.SqlClient.SqlInternalConnection.OnError( SqlException异常,Boolean breakConnection) 4846875
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)194 System.Data.SqlClient.TdsParser.Run(runBehavior runBehavior,SqlCommand的cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)2392
System.Data.SqlClient的。 SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +83
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient .SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data。 SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,String 方法) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为) +12 System.Data.Common.DbCommand.ExecuteReader() +12
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object [] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +975
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
System.Data.Linq.DataQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +35 System.Linq.Buffer1..ctor(IEnumerable 1..ctor(IEnumerable) 1..ctor(IEnumerable1 source) +247 System.Linq.<GetEnumerator>d__0.MoveNext() +108
System.Linq.Buffer1 source) +259
System.Linq.<GetEnumerator>d__0.MoveNext() +108
System.Collections.Generic.List1 collection) +7665172 System.Linq.Enumerable.ToList(IEnumerable1 源)+61 Mezza_crm.Controllers.ProjectsController.GetProjectList(NameValueCollection form) in C:\mezza_crm\mezza_crm\Controllers\ProjectsController.cs:164 Mezza_crm.Controllers.ProjectsController.List() in C:\mezza_crm\mezza_crm ProjectsController.cs:53
lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 个参数) +24 System.Web.Mvc.<>c__DisplayClassa.b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func2 parameters) +178
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary1 continuation) +2541 个过滤器,ActionDescriptor actionDescriptor,IDictionary`2 个参数)+192
System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
System.Web.Mvc.Controller.ExecuteCore() +126
System.Web.Mvc。 ControllerBase.Execute(RequestContext requestContext) +27 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151 System.Web .Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +75
回答by Joe
Check the Initial Catalog parameter in your connection string. It may be that your code is looking in the wrong database for the Projects object.
检查连接字符串中的初始目录参数。可能是您的代码在错误的数据库中查找 Projects 对象。
For example, if you have database syncing setup in such a way that only a subset of the master-database's tables are transferred, you can encounter this error if Linq to SQL is expecting all tables to be in the database pointed to by the connection string.
例如,如果您以这样一种方式设置数据库同步,即只传输主数据库表的一个子集,则如果 Linq to SQL 期望所有表都在连接字符串指向的数据库中,您可能会遇到此错误.
回答by Scott Ferguson
Do you have access to the SQL Server you are querying? Can you see a Table or View called dbo.Projects there? If not, that would be a good place to look.
您是否有权访问您正在查询的 SQL Server?你能在那里看到一个名为 dbo.Projects 的表或视图吗?如果没有,那将是一个看的好地方。
Linq to SQL creates an object map between the database and the application. If your new DLL that you're deploying doesn't match with the database anymore, then this is the sort of error you'd expect to get.
Linq to SQL 在数据库和应用程序之间创建对象映射。如果您正在部署的新 DLL 不再与数据库匹配,那么这就是您希望得到的那种错误。
Do you perhaps have different database schemas between your development environment and the deployment environment?
您的开发环境和部署环境之间可能有不同的数据库模式吗?
回答by Soumik Chowdhury
This maybe due to an incorrect table name from where you are fetching the data. Please verify the name of the table you mentioned in asmx file and the table created in database.
这可能是由于您从中获取数据的表名不正确。请验证您在 asmx 文件中提到的表的名称以及在数据库中创建的表。
回答by Chr Lorphelin
The cause of this problem could be a property setting of the database (Sql2008R2 with .NET4).
此问题的原因可能是数据库的属性设置(Sql2008R2 和 .NET4)。
- Problem is reproducible at will when changing the COLLATION value of a database.
- 更改数据库的 COLLATION 值时,问题可以随意重现。
To display COLLLATION, use the Sql Server Mgmt Studio.
要显示 COLLLATION,请使用 Sql Server Mgmt Studio。
- Right-click the database and select Properties -> General, Then look under Maintenancefor the COLLATION value
- 右键单击数据库并选择属性 -> 常规,然后在维护下查找COLLATION 值
To change COLLATION, (still) use the Sql Server Mgmt Studio.
要更改 COLLATION,(仍然)使用 Sql Server Mgmt Studio。
- Right-click the database and select Properties -> Options, From there, you can change the COLLATION value
- 右键单击数据库并选择属性 -> 选项,从那里,您可以更改 COLLATION 值
回答by CularBytes
If you are in that phase of development where you have an method inside your context class that creates testdata for you, don't call it in your constructor, it will try to create those test records while you don't have tables yet. Just sharing my mistake...
如果您处于开发的那个阶段,您的上下文类中有一个方法可以为您创建测试数据,请不要在构造函数中调用它,它会在您还没有表时尝试创建这些测试记录。只是分享我的错误...
回答by DeveloperOfDreams
The problem I had was because I had made a database in my LocalDb.
If that's the case then you have to write is as shown below:
我遇到的问题是因为我在 LocalDb 中创建了一个数据库。
如果是这种情况,那么您必须编写如下所示:
"SELECT * FROM <DatabaseName>.[dbo].[Projects]"
Replace with your database name.
You can probably also drop the "[ ]"
替换为您的数据库名称。
您也可以删除“[ ]”
回答by Salahuddin
Delete _MigrationHistory table in (yourdatabseName > Tables > System Tables) if you already have in your database and then run below command in package manager console
如果您的数据库中已经有,请删除 (yourdatabseName > Tables > System Tables) 中的 _MigrationHistory 表,然后在包管理器控制台中运行以下命令
PM> update-database
回答by StepUp
I had the same error. The cause was that I had created a table with wrong schema(it ought to be [dbo]). I did the following steps:
我有同样的错误。原因是我创建了一个模式错误的表(应该是[dbo])。我做了以下步骤:
I dropped all tables which does not have a prefix "dbo."
I created and run this query:
我删除了所有没有前缀“dbo”的表。
我创建并运行了这个查询:
CREATE TABLE dbo.Cars(IDCar int PRIMARY KEY NOT NULL,Name varchar(25) NOT NULL,
CarDescription text NULL)
GO
回答by Mohammad Dayyan
If you use two databases you can add another DataClasses.dbmland map the second database into it.
It works.
如果您使用两个数据库,您可以添加另一个DataClasses.dbml并将第二个数据库映射到其中。
有用。
回答by Ricardo Rodriguez
I have seen that the new versions when you define the resulting entities better define them in the following way if you handle a different scheme, I had a similar problem
我已经看到,如果您处理不同的方案,当您定义结果实体时,新版本可以更好地按以下方式定义它们,我遇到了类似的问题
You must add System.ComponentModel.DataAnnotations.Schema
您必须添加 System.ComponentModel.DataAnnotations.Schema
using System.ComponentModel.DataAnnotations.Schema;
[Table("InstitucionesMilitares", Schema = "configuracion")]


