views:

452

answers:

1

Anyone have some good examples (newb worthy) of what bulk inserting should look like or a great way to explain it, utilizing the whole LINQ to SQL method?

I've got a DB with about 7 tables I'm trying to link some .net pages to and as much as I can qry most of these tables, I'm having a heck of a time inserting into them. One complex scenerio I am finding revolves around the use of a GUID and how to get that particular GUID to propogate over into another table...

Anyone have any ideas/examples?

+1  A: 

You might want to give a little more insight to the nature of your problem and your current setup.

I haven't really messed with LINQ before so i just set up a really simple project. I was able to insert GUIDs into two seperate tables just fine.

    Dim db As New testDBDataContext

    ' Table1 ID (Primary Key)
    Dim gId As Guid = Guid.NewGuid()

    ' Table2 ID (Primary Key)
    Dim gId2 As Guid = Guid.NewGuid()

    ' Insert Record into Table 1
    Dim tb1Insert As New test_tb1 With {.Id = gId, .Name = "TestName"}
    db.test_tb1s.InsertOnSubmit(tb1Insert)

    ' Insert Record into Table 2, with testID referenced to Table1's Primary Key
    Dim tb2Insert As New test_tb2 With {.Id = gId2, .test1Id = gId, .OtherName = "OtherName"}
    db.test_tb2s.InsertOnSubmit(tb2Insert) '

    ' Commit Changes
    db.SubmitChanges()

The only way to get i got an error is if i set up a relationship in SQL Server between the two tables. Then if i tried to insert table 2 first it would error out because i would be trying to insert an GUID table 2's "test1ID" w/o having that GUID in table 1 yet.

If you are getting a "conflicted with COLUMN FOREIGN KEY constraint" error thats probably what is happening.

TheDPQ