SqlBulkCopy of EntiryFramework table

Santosh Umarani 81 Reputation points
2022-04-21T12:06:36.967+00:00

Hi,

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"
}
context.LogHistories.Add(log);
context.SaveChanges();
}

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";
bulkCopy.WriteToServer(dataTable);
}
}

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

Thanks,
Santosh

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 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.
12,814 questions
C#
C#
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,309 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-04-22T08:54:09.717+00:00

    @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);  
                connection.Open();  
                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());  
      
                    s.WriteToServer(table);  
                }  
                connection.Close();  
            }  
    

    Result in Database:

    195543-image.png

    Hope this could help you.

    Best Regards,
    Jack


    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