由于逐行反射相当昂贵,我一直在寻找构建和插入实体的更快的替代方案.我做了一些关于这个主题的研究,并且还发现了一些性能比较,这似乎表明表达树是要走的路.我将如何重写以下功能以利用此功能?
public static void InsertTable(IEnumerable<DataTable> chunkedTable)
{
Parallel.ForEach(
chunkedTable,
new ParallelOptions
{
MaxDegreeOfParallelism = Convert.ToInt32(ConfigurationManager.AppSettings["MaxThreads"])
},
chunk =>
{
Realty_Records_ProdEntities entities = null;
try
{
entities = new Realty_Records_ProdEntities();
entities.Configuration.AutoDetectChangesEnabled = false;
foreach (DataRow dr in chunk.Rows)
{
var parcelToInsert = new Parcel();
foreach (DataColumn c in dr.Table.Columns)
{
var propertyInfo = parcelToInsert.GetType()
.GetProperty(
c.ColumnName,
BindingFlags.SetProperty | BindingFlags.IgnoreCase
| BindingFlags.Public | BindingFlags.Instance);
propertyInfo?.SetValue(
parcelToInsert,
TaxDataFunction.ChangeType(
dr[c.ColumnName],
propertyInfo.PropertyType),
null);
}
entities.Parcels.Add(parcelToInsert);
}
entities.SaveChanges();
}
catch (Exception ex) …Run Code Online (Sandbox Code Playgroud) c# reflection entity-framework expression-trees parallel.foreach