Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records with different columns break SqlBulkInserter. #338

Open
mattrrichard opened this issue Mar 25, 2014 · 0 comments
Open

Records with different columns break SqlBulkInserter. #338

mattrrichard opened this issue Mar 25, 2014 · 0 comments

Comments

@mattrrichard
Copy link

SqlBulkInserter makes 2 bad assumptions about the records it gets in a bulk insert:

  1. All records contain the same columns (or at least that the first record has all of the columns since only the first is used to generate the data table and all 'optional' columns are at the end)
  2. Calling record.Values.ToArray() will yield the same column ordering on all records.
dynamic r1 = new SimpleRecord();
r1.FirstName = "Bob";
r1.LastName = "Dole";

dynamic r2 = new SimpleRecord();
r2.FirstName = "Bob";
r2.MiddleInitial = "L";
r2.LastName = "Saget";

db.People.Insert(new [] {r1, r2});

In the example above, when SqlBulkInserter tries to add the row for r2 with dataTable.Rows.Add(record.Values.ToArray()) it will generate an exception stating "input array is longer than the number of columns" because the table only has 2 columns.

If, however, you passed the records in the other order (db.People.Insert(new [] {r2, r1})), It wouldn't error, but r1's data wouldn't be correctly inserted: "Dole" would be in the MiddleInitial column instead of Last Name because the order of adding those values to the record effects the order of the values in the ToArray call.

@mattrrichard mattrrichard reopened this Mar 25, 2014
mattrrichard pushed a commit to mattrrichard/Simple.Data that referenced this issue Mar 25, 2014
markrendle added a commit that referenced this issue Mar 31, 2014
Fixes issue #338. (SqlBulkInserter breaks when the records aren't homogenous)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant