0

嗨,我无法解决此错误。任何有关该问题的帮助将不胜感激,谢谢!

错误信息:

存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目。

每当我尝试添加笔记本电脑/台式机时,都会收到上述错误消息。
在本地运行时一切正常,但在开发时却不行。网站和服务/数据库位于两个不同的开发箱上。

Tables:
Computer: ComputerID, UserID, HardwareName, Brand, IsDefaultDevice
Desktop: ComputerID,  MonitorWidth
Laptop: ComputerID, BatteryLife

generated sql:
exec sp_executesql N'insert [ScratchPad].[Computer]([UserID], [ComputerName], [Brand], [IsDefaultDevice])
values (@0, @1, @2, @3)
select [ComputerID]
from [ScratchPad].[Computer]
where @@ROWCOUNT > 0 and [ComputerID] = scope_identity()',N'@0 bigint,@1 nvarchar(19),@2 int,@3 bit',@0=2,@1=N'Computer666',@2=1,@3=0


using(var context = new MyDatabaseEntities())
{
       User user = context.Users.FirstOrDefault(x => x.UserID == userId);
       entityToAdd.User = user;
       bool hasOthers = context.Computers.Any(x=>x.User.UserID == userId);
       if(!hasOthers && !entityToAdd.IsDefaultDevice)
            entityToAdd.IsDefaultDevice = true;
       entityToAdd.BrandReference.EntityKey = Brand.GetDellProviderKey();
       context.AddToComputers(entityToAdd);
       context.SaveChanges();
}

这是堆栈跟踪:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[FaultException`1: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.]
   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10259418
   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +539
   myWebPortal.Repositories.UserServiceRef.IUserService.AddComputer(Int64 userId, Computer toAdd) +0
   myWebPortal.Repositories.UserServiceRef.UseServiceClient.AddComputer(Int64 userId, Computer toAdd) in c:\users\katelyn\documents\my web project\myWebPortal.repositories\service references\userserviceref\reference.cs:1282
   myWebPortal.Repositories.UserAccountRepository.AddComputer(Int64 userId, Computer computer) in C:\Users\katelyn\Documents\my Web Project\myWebPortal.Repositories\UserRepository.cs:238
   myWebPortal.Web.Controllers.ComputerController.AddComputer(ComputerModel model) in C:\Users\katelyn\Documents\my Web Project\myWebPortal.Web\Controllers\ComputerController.cs:71
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +69
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +236
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +31
   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +85
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +632195
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +288
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +630660
   System.Web.Mvc.Controller.ExecuteCore() +125
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +48
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +15
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +85
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +454
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +263

此外,在尝试添加笔记本电脑/台式机时,有时会插入计算机行,但不会插入笔记本电脑/台式机行。

4

2 回答 2

1

我以前遇到过同样的问题,它是由 SQL Server 中的 INSTEAD OF 触发器引起的。在我看来,当 SQL Server 报告查询成功完成时会发生 OptimisticConcurrencyExceptions,但正在进行的 SELECT 语句未返回预期的行数。

@mrunge在@BasicLife 的答案的评论中提供了我针对该问题发布的解决方案的链接。

另一个潜在原因是用于插入和更新实体的存储过程。来自http://msdn.microsoft.com/en-us/library/bb738618.aspx

当您定义使用存储过程对数据源进行更新的实体数据模型时,也会发生 OptimisticConcurrencyException。在这种情况下,当用于执行更新的存储过程报告零行已更新时,将引发异常。

我相当确定在正常执行由 发出的 SQL 查询期间发生的实际 SQL 错误(键违规、空错误等)将使用与您看到的不同的异常来报告。我不记得异常的名称。

于 2011-06-22T00:14:05.523 回答
1

你在哪里实例化/初始化entityToAdd

您能确认外键约束和主键信息(自动增量、类型等)吗?似乎插入因某种原因而失败,因为受影响的行数不正确。

尝试从 sql 管理控制台中自己运行 SQL - 你有任何错误吗?记录是否正确插入?

您可能还想确保表上没有可能导致问题的触发器或类似内容。

于 2010-09-20T22:24:00.060 回答