0

i am trying to insert data into database table using sqlBulkCopy it works fine data is inserted, then I would like to query my data but when I referenced to the updated table, I am getting error: object is null. I am able to see this table using entity structure but value of this object is null:

      using (var cn = new AdventureWorksEntities())
        {
            cn.Database.ExecuteSqlCommand("TRUNCATE TABLE tmpTable");
            sqlConnectionString = cn.Database.Connection.ConnectionString;
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
          {
            bulkCopy.DestinationTableName = "dbo.tmpTable";
            try
            {
                // Write from the source to the destination.
                bulkCopy.WriteToServer(table);
            }
            catch (Exception ex)
            {
                //ErrorHandler
            }
          }
        // Now I am trying to get data from the updated table:

            var uniqueItemIDs = cn.tmpTable.Select(m => m.ItemID).Distinct();
        // And from the line above I am getting the Error: tmpTable is null.
        }

other tables (that weren't updated using SqlBulkCopy) are fine and accessible. Can somebody help?

1 Answers1

1

I believe the problem is you need to refresh your DB context. You open the DB context prior to doing the insert.

Wjdavis5
  • 3,952
  • 7
  • 35
  • 63