C# 实体框架更新

C# 实体框架更新,c#,wpf,entity-framework,C#,Wpf,Entity Framework,更改表中的某些字段时无法更新 数据库==>mysql .net framework==>4.0 平台==>wpf应用程序 让我一步一步地解释 我的目标是更新表中已经存在的项 public bool SaveToVideoInfo(List<tblvideoinfo> vList,sbyte profilID) { foreach (tblvideoinfo _videoinfo in vList) {

更改表中的某些字段时无法更新

  • 数据库==>mysql
  • .net framework==>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;                     
    }
因为若它存在于表中,我想像上面写的那个样更改一些字段。在运行ent.SaveChanges()之前,一切正常

我检查了UpdateItem是否已更改,并设置了属性(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()
下面是UpdateItem的详细信息

意味着

    updatingItem.SearchKeywords = vInfo.SearchKeywords;
    updatingItem.SearchTimeStamp = DateTime.Now;
这些代码是有效的

同时,VideoID在我的表中是唯一的,因此我不能添加具有相同VideoID的项。但我想更新它。我认为更新问题来自这里

我不想使用sql命令


我只想更新表中的两个字段…

是否无法检查该项是否为新项?您可以将
id
设置为0值或其他值,以便轻松查看项目是否为新项目

如果不可能,您可以在Id上使用
SingleOrDefault
,检查实体是否存在。这样可以避免updateException,并在一次调用中处理所有内容。依赖于此场景的数据库异常并不是一个非常干净的解决方案


在一次调用中处理所有内容应该可以解决您的问题。

我的猜测是,当您在同一上下文中再次调用SaveChanges时,它正在尝试插入。请重复插入操作,然后再次失败

在catch块的开头调用
ent.AcceptAllChanges()
可能有效

也许处理上下文并创建另一个上下文以在更新函数中使用是最简单的选择。否则,您需要将您尝试插入的两个实体(videoinfo、process)的
ObjectStateEntry.State
更改为
Unchanged

尝试调用

var updatingItem = (from a in ent.tblvideoinfo
                  where a.VideoID == vInfo.VideoID
                  select a).FirstOrDefault();
你正在使用

 updatingItem.SearchTimeStamp = DateTime.Now;
您的数据库是否对此字段使用相同的数据类型(datetime)


一个选项是处理
ObjectContext.SavingChanges
事件,这使您有机会在保存更改之前对实体执行验证,甚至在必要时取消保存。通过这种方式,您可以确保在尝试保存更改之前设置了任何不可为null的属性,并避免依赖异常处理。

内部异常的描述是什么?我已经在“此处是详细信息”下写过了,这是堆栈跟踪而不是描述HH抱歉。。这里有重复条目“18078437”,用于我看到的键“VideoID_UNIQUE”,但问题是如何更新此项?不可能吗?在sql中,我会写到“更新TBLVideInfo集SearchKeywords='bla bla'和SearchTimeStamp='bla bla'where videoID='blabla…'”加载一个项并在ObjectContext范围内修改它会自动跟踪更改。调用SaveChanges将生成更新Sqlthanx。可接受的变化();这是为了消除错误。但此时,savechanges方法返回0,在表中,没有任何更改..您是否尝试将该调用放在catch块的开头?没有,我没有:),但它可以工作。你太棒了:)非常感谢:)我从catch block开始就写了,并且工作:)我已经可以保存物品了。但如果表中的项目具有相同的videoid,我无法更新它。但是@Omtara解决了这个问题,请注意。我对更新项目没有任何问题,正如我提到的,它很好。但没关系,不管用,没问题:)谢谢
 updatingItem.SearchTimeStamp = DateTime.Now;