Actualización: Esto realmente no funcionó. ¡Ver comentarios!
Odio cuando busco en StackOverflow y la respuesta es: "¡No se puede!" o "Podrías, pero solo si cambias por completo cada llamada que hayas hecho".
Reflexión anyone? Esperaba que fuera una configuración de DbContext. Pero como no lo es, hice uno usando la reflexión.
Este pequeño y útil método configurará AsNoTracking en todas las propiedades del tipo DbSet.
private void GloballySetAsNoTracking()
{
var dbSetProperties = GetType().GetProperties();
foreach (PropertyInfo pi in dbSetProperties)
{
var obj = pi.GetValue(this, null);
if (obj.GetType().IsGenericType && obj.GetType().GetGenericTypeDefinition() == typeof(DbSet<>))
{
var mi = obj.GetType().GetMethod("AsNoTracking");
mi.Invoke(obj, null);
}
}
}
Agréguela a un constructor de DbContext sobrecargado.
public ActivationDbContext(bool proxyCreationEnabled, bool lazyLoadingEnabled = true, bool asNoTracking = true)
{
Configuration.ProxyCreationEnabled = proxyCreationEnabled;
Configuration.LazyLoadingEnabled = lazyLoadingEnabled;
if (asNoTracking)
GloballySetAsNoTracking();
}
Utiliza la reflexión, lo que significa que alguien comentará rápidamente que se trata de un golpe de rendimiento. Pero, ¿realmente es un gran éxito? Depende de tu caso de uso.
su enlace muéstrame el error –
@AliYousefie gracias, lo he solucionado. –