Entity Framework BulkInsert Temp table without creating

siva_dev 1 Reputation point
2023-09-27T13:21:04.1433333+00:00

Hi,

I am creating Temp Table and inserting data with EF BulkInsert , then I use that temp table in the dynamic script.

I need to BulkInsert 100K rows into SQL Server but it was keep loading . I tried to insert insert for loop but I am getting a error "Table already exist"

How to BulkInsert into temp table without creating temp ( from second iteration ) table using EF BulkInsert.

If not possible by using BulkInsert, any better way to create temp table with large amount of rows with same DBTransaction

I am using .NET 4.5 , EF 6

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,860 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,489 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenbin Geng 726 Reputation points Microsoft Vendor
    2023-09-28T08:04:28.1566667+00:00

    Hi @siva_dev , Welcome to Microsoft Q&A,

    Another method is to use SqlBulkCopy, the SqlBulkCopy Class allows you to efficiently bulk load SQL Server tables using data from other sources.

    The code is as follows:

              /// <summary>
             /// Batch insert
             /// </summary>
             /// <typeparam name="T">Type of generic collection</typeparam>
             /// <param name=" dbContext ">Connection object</param>
             /// <param name="tableName">Insert the generic collection into the table name of the local database table</param>
             /// <param name="list">To insert a large generic collection</param>
             public static bool BulkInsert<T>(DbContext dbContext, string tableName, IList<T> list)
             {
                 try
                 {
                     if (list == null || list.Count == 0) return true;
                     if (dbContext.Database.Connection.State != ConnectionState.Open)
                     {
                         dbContext.Database.Connection.Open(); //Open Connection connection
                     }
                     using (var bulkCopy = new SqlBulkCopy(dbContext.Database.Connection.ConnectionString))
                     {
                         bulkCopy.BatchSize = list.Count;
                         bulkCopy.DestinationTableName = tableName;
     
                         var table = new DataTable();
                         var props = TypeDescriptor.GetProperties(typeof(T))
     
                             .Cast<PropertyDescriptor>()
                             .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System"))
                             .ToArray();
     
                         foreach (var propertyInfo in props)
                         {
                             bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
                             table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
                         }
     
                         var values = new object[props.Length];
                         foreach (var item in list)
                         {
                             for (var i = 0; i < values.Length; i++)
                             {
                                 values[i] = props[i].GetValue(item);
                             }
                             table.Rows.Add(values);
                         }
     
                         bulkCopy.WriteToServer(table);
                         if (dbContext.Database.Connection.State != ConnectionState.Closed)
                         {
                             dbContext.Database.Connection.Close(); //Close Connection
                         }
                         return true;
                     }
                 }
                 catch (Exception ex)
                 {
                     if (dbContext.Database.Connection.State != ConnectionState.Closed)
                     {
                         dbContext.Database.Connection.Close(); //Close Connection
                     }
                     return false;
                 }
          }
    
    
    

    Best Regards,

    Wenbin

    If the answer is the right solution, please click "Accept Answer" and kindly 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

Your answer

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