[英]Why Linq “where” expression after Select gets evaluated locally when created through a generic method?
[英]Expression Tree in extension method will be evaluated locally
我想將TenantId添加到Asp.Net Identity表(例如:User)。
以下代碼片段可以正常工作。 租戶上下文將通過DI注入,並且租戶將根據http上下文域進行更改:
private readonly ITenantContext<ApplicationTenant> tenantContext;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, ITenantContext<ApplicationTenant> tenantContext) : base(options)
{
this.tenantContext = tenantContext;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(b =>
{
// add tenant
b.Property(typeof(int), "TenantId");
b.HasQueryFilter(x => EF.Property<int>(x, "TenantId") == this.tenantContext.Tenant.Id);
});
}
為了重用,我想為entityBuilder創建一個擴展方法:
public static class EntityTypeBuilderExtensions
{
public static void AddTenancy<TEntity>(
this EntityTypeBuilder<TEntity> builder,
Expression<Func<int>> tenantId,
string propertyName = "TenantId")
where TEntity : class
{
// validate
Ensure.Argument.NotNull("builder", builder);
// add property to entity
builder.Property(typeof(int), propertyName).IsRequired();
/* THIS WORKS BUT WILL BE EVALUATED LOCALLY */
// left
var parameterExp = Expression.Parameter(typeof(TEntity), "x"); // e = TEntity => e.g: User
var propertyNameExp = Expression.Constant(propertyName, typeof(string)); // the name of the tenant column - eg.: TenantId
// right
var tTenantId = Expression.Convert(tenantId.Body, typeof(int)); // tenantId
var propertyMethod = typeof(EF).GetMethod(nameof(EF.Property), BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(typeof(int)); // build EF.Property
var propertyMethodExec = Expression.Call(propertyMethod, parameterExp, propertyNameExp); // represents EF.Property(e, "TenantId")
var bodyExp = Expression.Equal(propertyMethodExec, tTenantId);
var lambda = Expression.Lambda(bodyExp, parameterExp);
builder.HasQueryFilter(lambda);
}
}
並在數據庫上下文中:
private Func<int> tenantId => () =>
{
// return tenant id
if (this.tenantContext != null && this.tenantContext.Tenant != null)
{
return this.tenantContext.Tenant.Id;
}
return -1;
};
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(b =>
{
b.AddTenancy(() => this.tenantId(), "TenantId");
});
}
擴展方法也可以,但是表達式是在本地求值的:-(。有人可以幫我修復它嗎?
LINQ表達式'where(Property([x],“ TenantId”)== Invoke(__ ef_filter__tenantId_0))'無法翻譯,將在本地進行評估。 LINQ表達式'where([x] .NormalizedUserName == __normalizedUserName_0)'無法翻譯,將在本地進行評估。 LINQ表達式“ FirstOrDefault()”無法翻譯,將在本地進行評估。
問題是這里的Func
private Func<int> tenantId => ...
這會導致翻譯Invoke(__ef_filter__tenantId_0))
和客戶評估錯誤。
解決方案是使tenantId
簡單int
返回屬性或方法。 例如,保持通話
b.AddTenancy(() => this.tenantId(), "TenantId");
它應該更改為
private int tenantId()
{
// return tenant id
if (this.tenantContext != null && this.tenantContext.Tenant != null)
{
return this.tenantContext.Tenant.Id;
}
return -1;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.