我一直在阅读 linq-to-sql CRUD 操作,据我所知,我正在以正确的方式解决这个问题,但细节上有问题。
我的笔记集没有以任何方式连接到数据上下文。它包含需要删除然后插入的项目。
List<NoteItem> notes = ConvertJsonToModel(apiResponse);
这很好用......
foreach (NoteItem note in notes)
{
Db.NoteItems.InsertOnSubmit(note);
}
Db.SubmitChanges();
但是, Db.NoteItems 已经填充,所以我需要更新存在的项目。现在为了简单起见,在我执行上述插入之前,我正在尝试对现有项目执行删除(我将在我掌握删除后让更新工作)。
foreach (NoteItem note in notes)
{
Db.NoteItems.Attach(note);
Db.NoteItems.DeleteOnSubmit(note);
}
Db.SubmitChanges();
我得到的错误是:
FormatException 未处理
这很奇怪,因为我可以插入完全相同的注释,但我不能删除它。
NoteItem 有 3 个属性:字符串 Id、DateTime ModifiedAt、字符串 Text,以及 PropertyChanging 和 PropertyChanged 事件。
更新
我刚刚发现所有记录都不会发生错误,因此数据也是特定的。然而,相同的数据对于插入来说效果很好。
堆栈跟踪
System.FormatException was unhandled
Message=FormatException
StackTrace:
at System.Int32.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.Convert.ToInt32(String value, IFormatProvider provider)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Data.Linq.DBConvert.ChangeType(Object value, Type type)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.BuildKeyValues(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetDelete(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Delete(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at DrivenNotes.ViewModels.NotesViewModel.OnOnePageUpdated(Object sender, StreamCompleteEventArgs e)
at DrivenNotes.ApiCommunication.ApiComms.OnGotStreamPage(Object sender, DownloadStringCompletedEventArgs e)
at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)