嗨,我无法解决此错误。任何有关该问题的帮助将不胜感激,谢谢!
错误信息:
存储更新、插入或删除语句影响了意外数量的行 (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
此外,在尝试添加笔记本电脑/台式机时,有时会插入计算机行,但不会插入笔记本电脑/台式机行。