我試過了,它確實對性能有一點點改善......有時。
protected override YourEnityContextHere CreateDataSource()
{
YourEnityContextHere dataSource = base.CreateDataSource();
TurnOffChangeTracking(dataSource);
return dataSource;
}
static IEnumerable<PropertyInfo> objSetProps;
private static void TurnOffChangeTracking(YourEnityContextHere dataSource)
{
if(objSetProps == null)
objSetProps = dataSource.GetType().GetProperties().Where(prop => prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(ObjectSet<>));
foreach (PropertyInfo objSetProp in objSetProps)
{
ObjectQuery objSet = (ObjectQuery)objSetProp.GetValue(dataSource, BindingFlags.GetProperty, null, null, null);
objSet.MergeOption = MergeOption.NoTracking;
}
}