Table object is null after sqlbulkcopy insert

.net c# sql sqlbulkcopy

Question

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
0
10/1/2014 11:02:00 PM

Popular Answer

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

1
10/1/2014 11:48:42 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow