SqlBulkCopy of EntiryFramework table

Santosh Umarani 81 Reputation points


I have a LogHistory table which is available in Entity framework. I have set of values to be set in this table. If I set like below for individual values, I am able to set the values in LogHistory table.

using (var context = new AutomationEntities())
var log = new LogHistory
UserName = GetUserName(),
UserId = GetUserId(),
ActionPerformed = "EditTestCase"

However, I wanted to set these values using bulkcopy. I have all the data in "dataTable". The updated values are being set in LogHistory table. I guess I am setting DestinationTableName wrongly.

using (var context = new AutomationEntities())
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
bulkCopy.DestinationTableName = "dbo.LogHistory";

Can you please suggest how should I set DestinationTableName in this case ? Kindly waiting for your response.


Microsoft Technologies based on the .NET software framework.
3,598 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,270 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,604 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor

    @Santosh Umarani , Welcome to Microsoft Q&A, based on my test, there is no error for me if you want to copy data from datatable to database.

    I make a code example and you could refer to it.

     static void Main(string[] args)  
               TestEntities1 test=new TestEntities1();  
                DataTable table = GetDataTable();  
                SqlConnection connection = new SqlConnection(test.Database.Connection.ConnectionString);  
                using (SqlBulkCopy s = new SqlBulkCopy(connection))  
                    //set the table name  
                    s.DestinationTableName = "dbo.LogHistory";  
                    foreach (var column in table.Columns)  
                        s.ColumnMappings.Add(column.ToString(), column.ToString());  

    Result in Database:


    Hope this could help you.

    Best Regards,

    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful