LINQ的DataContext的一个捕获的异常后,没有恢复? [英] Linq DataContext doesn't recover after a caught exception?

查看:112
本文介绍了LINQ的DataContext的一个捕获的异常后,没有恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ的第一次,我遇到了一些问题,我不完全与DataContext的理解。希望计算器可以提供帮助。

I am using LINQ for the first time and I am running into some behavior I don't fully understand with the DataContext. Hopefully StackOverflow can help.

基本上,它看起来像曾经我的DataContext遇到异常,它要么缓存的异常或没有完全恢复。在我的特定情况下,一旦我试图传递一个字符串,它是太大,我的数据库,它会抛出同样的异常对后面的所有字符串。

Basically, it seems like once my DataContext encounters an exception, it either caches that exception or never fully recovers. In my specific case, once I try to pass a string that is too large to my DB, it throws the same exception for all following strings.

我有一个表是这样的:

 DisplayString(
  StringID nvarchar(255)
  ,CultureCode varchar(25)
  ,DisplayString nvarchar(255)
 )

在.NET中创建一个DataContext我映射到这个表与LINQ to SQL类。

In .Net I create a DataContext which I map to this table with a LINQ to SQL class.

Dim context As New LinqTableMappingDataContext(
  My.Settings.LocalizationStringsConnectionString)

我再创建一批测试字符串,遍历每个并将其发送到我的餐桌。

I then create a bunch of test strings, iterate through each and send them to my table.

Dim arr As String() = 
  {"aaaaa",  "adsfs", "wefwf", "dfgsfg", "ergsdfg", 
   "fsdgergd", "Sdgdfgegd", "ergdfgsfd"}

  For Each a As String In arr
     addToLinq(a)
  Next

这工作得很好。但是,如果我添加下面的字​​符串在索引1,所有后续插入失败,抛出的长字符串相同的异常(SQLEXCEPTION)。

This works fine. However if I add the following string at index 1, All subsequent inserts fail, throwing the same exception (SqlException) as the long string.

"WARNING: This computer program is protected by copyright law and international
treaties. Unauthorized reproduction of this program, or any portion of it, may
result in severe civil and criminal penalties, and will be prosecuted to the
maximum extent possible under the law."

下面是我的AddToLinq()方法。

Here is my AddToLinq() method.

Private Sub AddToLinq(ByVal stringID As String)

  Dim f As New DisplayString
  Try

     Dim count As Integer = (From str In context.DisplayStrings _
       Where str.StringID = stringID Select str).Count

     If count = 0 Then

        f.StringID = stringID
        f.CultureCode = "en-US"
        f.DisplayString = stringID

        context.DisplayStrings.InsertOnSubmit(f)
        context.SubmitChanges()
        Console.WriteLine("SUBMITTED: " & stringID)
    End If

  Catch ex1 As SqlException
     Console.WriteLine("------------------------------------------------")
     Console.WriteLine("EXCEPTION: " & ex1.Message)
     Console.WriteLine(stringID)
     Console.WriteLine(stringID.Length)
     Console.WriteLine("------------------------------------------------")

  Catch ex As DuplicateKeyException
     Console.WriteLine(ex.Message)

  End Try
End Sub

如果有人可以帮助我,将是巨大的,谢谢。

If anyone could help me that would be great, thanks.

推荐答案

如果你观察到的您的DataContext的GetChangeSet方法,你会看到,它仍然要插入第一个字符串。这就是为什么它不会恢复。

If you observe the GetChangeSet method of your DataContext, you'll observe that it still wants to insert the first string. This is why it doesn't recover.

这篇关于LINQ的DataContext的一个捕获的异常后,没有恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆