Share via

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

Developer technologies | .NET | Other
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | C#
Developer technologies | 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.


Answer accepted by question author

Jack J Jun 25,306 Reputation points
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.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.