我一直在使用FastMember's objectreader和和這樣的columnmapping解決它:
List<Data> lstData = new List<Data>();
//fill list of data..
using (var sqlCopy = new SqlBulkCopy(Config.get("Data:db:ConnectionString")))
{
sqlCopy.DestinationTableName = "[tblBulkCopy]";
sqlCopy.BatchSize = 500;
//map property index to column index in table. in database table the first column is the identitycolumn
sqlCopy.ColumnMappings.Add(0, 1);
sqlCopy.ColumnMappings.Add(1, 2);
sqlCopy.ColumnMappings.Add(2, 3);
var records = lstData.Select(obj => new { col1 = obj.number1, col2 = obj.number2, col3 = obj.number3 });
using (var reader = ObjectReader.Create(records, new[] { "col1", "col2", "col3" }))
{
sqlCopy.WriteToServer(reader);
}
}
試試這個庫:https://github.com/borisdj/EFCore.BulkExtensions – borisdj