• EF大数据插入


    _April给出代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
     
    namespace MMAS_Demo
    {
     
        public class DbContextExtension
        {
            System.Data.Common.DbConnection Connection = null;
     
            public DbContextExtension(System.Data.Common.DbConnection connection)
            {
                Connection = connection;
            }
     
            public void BulkInsertAll<T>(IEnumerable<T> entities)  //大数据保存,针对一次插入几千条数据的情况
            {
                entities = entities.ToArray();
     
                string cs = Connection.ConnectionString;
                var conn = new SqlConnection(cs);
                conn.Open();
     
                Type t = typeof(T);
     
                var bulkCopy = new SqlBulkCopy(conn)
                {
                    DestinationTableName = t.Name
                };
     
                var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
                var table = new DataTable();
     
                foreach (var property in properties)
                {
                    Type propertyType = property.PropertyType;
                    if (propertyType.IsGenericType &&
                        propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        propertyType = Nullable.GetUnderlyingType(propertyType);
                    }
     
                    table.Columns.Add(new DataColumn(property.Name, propertyType));
                }
     
                foreach (var entity in entities)
                {
                    table.Rows.Add(properties.Select(
                      property => GetPropertyValue(
                      property.GetValue(entity, null))).ToArray());
                }
     
                bulkCopy.WriteToServer(table);
                conn.Close();
            }
     
            private bool EventTypeFilter(System.Reflection.PropertyInfo p)
            {
                var attribute = Attribute.GetCustomAttribute(p,
                    typeof(AssociationAttribute)) as AssociationAttribute;
     
                if (attribute == null) return true;
                if (attribute.IsForeignKey == false) return true;
     
                return false;
            }
     
            private object GetPropertyValue(object o)
            {
                if (o == null)
                    return DBNull.Value;
                return o;
            }
        }
    }
    

      

  • 相关阅读:
    [学习笔记&教程] 信号, 集合, 多项式, 以及各种卷积性变换 (FFT,NTT,FWT,FMT)
    [学习笔记] CDQ分治&整体二分
    [日常] NOIp 2018 滚粗记
    [学习笔记] 模拟退火 (Simulated Annealing)
    [日常] NOIWC 2018爆零记
    [日常] PKUWC 2018爆零记
    [日常] 最近的一些破事w...
    [BZOJ 1877][SDOI2009]晨跑
    [COGS 2583]南极科考旅行
    [日常] NOIP 2017滚粗记
  • 原文地址:https://www.cnblogs.com/lxny/p/6547310.html
Copyright © 2020-2023  润新知