I am using LINQ for the first time, and I am encountering some kind of behavior that I do not quite understand with DataContext. Hope StackOverflow can help.
Basically, it seems that when my DataContext encounters an exception, it either caches this exception, or it never gets completely recovered. In my specific case, when I try to pass a row that is too large for my database, it throws the same exception for all the following rows.
I have a table as such:
DisplayString( StringID nvarchar(255) ,CultureCode varchar(25) ,DisplayString nvarchar(255) )
In .Net, I create a DataContext that I map to this table with the LINQ to SQL class.
Dim context As New LinqTableMappingDataContext( My.Settings.LocalizationStringsConnectionString)
Then I create a bunch of test rows, iterate over them and send them to the table.
Dim arr As String() = {"aaaaa", "adsfs", "wefwf", "dfgsfg", "ergsdfg", "fsdgergd", "Sdgdfgegd", "ergdfgsfd"} For Each a As String In arr addToLinq(a) Next
It works great. However, if I add the next row to index 1, all subsequent inserts fail, throwing the same exception (SqlException) as a long row.
"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."
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.