【问题标题】:Invalid ObjectName xxx.dbo.xxx, but it is there无效的 ObjectName xxx.dbo.xxx,但它在那里
【发布时间】:2015-08-20 18:05:34
【问题描述】:

我不明白,为什么下面的代码不起作用:

        //Database connection
        using (var db = new KassenautomatEntities())
        {
            //Create Model
            var quittung = new WAWI_Quittungen
            {
                QuittungText = "WebInterface",
                QuittungBetrag = amount,
                QuittungDatum = DateTime.Now,
                KartenIDKassierer = 0,
                KartenIDKunde = userInfo.CardId,
                Zahlungsart = 4,
                ClientNr = WebInterfaceInfo.ClientId,
                LastPrintDate = null,
                PrintedCount = 0,
                StornoQuittungID = null,
                Status = 0
            };

            //Save Model
            db.WAWI_Quittungen.Add(quittung);
            db.SaveChanges(); //<----- Crash

我收到此错误:

无效的对象名“KasseJournal.dbo.WAWI_Quittungen”

一般每张桌子都适合我。这是唯一没有的。

WAWI_Quittungen 有一个名为QuittungID 的自动增量主键,我在这里没有定义,但我不认为这是问题所在。

我还从图表中删除了表格并从数据库中添加了它。

兴趣点:

  • 在 SQL Studio 中,它被命名为 [Kassenautomat].[dbo].[WAWI_Quittungen] 而不是 KasseJournal.dbo.WAWI_Quittungen
  • 如果我通过实体框架从这个表中读取,我没有问题。
  • 我在整个解决方案中对 KasseJournal 进行了文本搜索,但结果为零。

当然,我用谷歌搜索了很多。我没有这样的问题之一:

  • 数据库触发器
  • 连接了错误的数据库
  • 复数化新对象设置为假

我错过了什么?


连接字符串:

 <add name="KassenautomatEntities" connectionString="metadata=res://*/Models.Database.KassenautomatModel.csdl|res://*/Models.Database.KassenautomatModel.ssdl|res://*/Models.Database.KassenautomatModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Kassenautomat;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

工作生成的 SQL:

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[WAWI_Quittungen] AS [Extent1]
        WHERE [Extent1].[KartenIDKunde] = @p__linq__0
    )  AS [GroupBy1]

不工作的 SQL:

INSERT [dbo].[WAWI_Quittungen]([QuittungText], [QuittungBetrag], [...])
VALUES (@0, @1, @2, @3, @4, @5, @6, NULL, @7, NULL, @8)
SELECT [QuittungID]
FROM [dbo].[WAWI_Quittungen]
WHERE @@ROWCOUNT > 0 AND [QuittungID] = scope_identity()

堆栈跟踪:

[SqlException (0x80131904): Ungültiger Objektname 'KasseJournal.dbo.WAWI_Quittungen'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1787814
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5341674
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1693
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +377
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +137
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c) +9
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +72
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) +402
   System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) +166
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) +234
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +159

[UpdateException: Fehler beim Aktualisieren der Einträge. Weitere Informationen finden Sie in der internen Ausnahme.]
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +334
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut) +9
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T noChangesResult, Func`2 updateFunction) +132
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +105
   System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() +11
   System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288
   System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) +157
   System.Data.Entity.Core.Objects.<>c__DisplayClass2a.<SaveChangesInternal>b__27() +21
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +162
   System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) +221
   System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) +6
   System.Data.Entity.Internal.InternalContext.SaveChanges() +113

[DbUpdateException: Fehler beim Aktualisieren der Einträge. Weitere Informationen finden Sie in der internen Ausnahme.]
   System.Data.Entity.Internal.InternalContext.SaveChanges() +196
   System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +26
   System.Data.Entity.DbContext.SaveChanges() +20
   WebInterface.Controllers.RechargeCardController.HandleSuccessPayment(Decimal amount, UserInfo userInfo) in d:\Daten\Dev\Conwell\WebInterface\Controllers\RechargeCardController.cs:336
   WebInterface.Controllers.RechargeCardController.PaymentExecute(PaymentInitViewModel model) in d:\Daten\Dev\Conwell\WebInterface\Controllers\RechargeCardController.cs:222
   lambda_method(Closure , ControllerBase , Object[] ) +180
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +434
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
   System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651796
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

  • 其他工作的表是否使用相同的 DbContext?当您从“工作”表跟踪 EF 生成的 SQL 时,它会显示“KasseJournal”还是“Kassenautomat”?你能告诉我们你的连接字符串吗?
  • 我对 EF 比较陌生。如何跟踪生成的 SQL?将在一分钟内发布连接字符串。
  • 谢谢@sstan 我更新了我的问题。它只显示 dbo.table
  • 很奇怪。您是否尝试过在 SQL Management Studio 中手动运行生成的插入语句?根据您迄今为止提供的信息,它似乎是特定于数据库的,并且可能根本与 EF 无关。但是做这个测试会证实这一点。

标签: c# entity-framework entity-framework-6 dbcontext savechanges


【解决方案1】:

好的,我想通了。我给你的信息有误,很抱歉。

这是一个触发器。

我正在研究数据库触发器而不是表触发器。我不知道 MsSQL 对每个表都有一个触发器定义。在 MySQL 中,它是一个很大的 Trigger Table。

所以我删除了触发器,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多