当我更改表中的某些字段时,我无法更新。
- 数据库 ==> mysql
- .net 框架 ==> 4.0
- 平台==> wpf应用
让我一步一步解释
我的目标是更新表中已经存在的项目。
public bool SaveToVideoInfo(List<tblvideoinfo> vList,sbyte profilID)
{
foreach (tblvideoinfo _videoinfo in vList)
{
try
{
ent.AddTotblvideoinfo(_videoinfo);
ent.AddTotblprocess(CreateProcess(profilID,_videoinfo.VideoID));
ent.SaveChanges();
}
catch (UpdateException )
{
return UpdateToVideoInfo(_videoinfo);
}
}
return false;
}
如果表中已经存在项目,我将捕获此异常并调用我的更新函数
public bool UpdateToVideoInfo(tblvideoinfo vInfo)
{
var updatingItem = (from a in ent.tblvideoinfo
where a.VideoID == vInfo.VideoID
select a).First();
updatingItem.SearchKeywords = vInfo.SearchKeywords;
updatingItem.SearchTimeStamp = DateTime.Now;
return ent.SaveChanges() > 0;
}
因为如果它存在于表中,我想改变我上面写的一些字段。在运行 ent.SaveChanges() 之前一切都很好
我检查了我设置属性(SearchKeywords,SearchTimeStamp)的更新项目已更改
但是出现这个错误
更新条目时发生错误。有关详细信息,请参阅内部异常
这是详细信息
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at MyDbHelper.DBHelper.UpdateToVideoInfo(tblvideoinfo vInfo)
at MyDbHelper.DBHelper.SaveToVideoInfo(List`1 vList, SByte profilID)
at youtube.MainWindow.p_Drop_Event(Object sender, Object to) in C:\Users\xxxxxx....\...\MainWindow.xaml.cs:line 1125
at youtube.product.UserControl_MouseUp(Object sender, MouseButtonEventArgs e) in C:\Users\xxxxxx....\...\product.xaml.cs:line 239
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at youtube.App.Main() in C:\Users\xxxxxx....\...\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这是updatedItem的详细信息
方法,
updatingItem.SearchKeywords = vInfo.SearchKeywords;
updatingItem.SearchTimeStamp = DateTime.Now;
这些代码是有效的。
同时,VideoID 在我的表中是唯一的,然后我无法添加具有相同 VideoID 的项目。但我想更新它。我认为更新问题来自这里。
我不想使用 sql 命令。
我只想更新表的两个字段...