我想將 TenantId 添加到 Asp.Net 身份表(例如:用戶)。以下代碼段工作正常。租戶上下文將通過(guò) DI 注入,租戶根據(jù) http 上下文域進(jìn)行更改: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 創(chuàng)建一個(gè)擴(kuò)展方法: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 }}擴(kuò)展方法也可以正常工作,但表達(dá)式是在本地評(píng)估的:-(。有人可以幫我修復(fù)它嗎?LINQ 表達(dá)式 'where (Property([x], "TenantId") == Invoke(__ef_filter__tenantId_0))' 無(wú)法翻譯,將在本地進(jìn)行評(píng)估。無(wú)法翻譯 LINQ 表達(dá)式“where ([x].NormalizedUserName == __normalizedUserName_0)”并將在本地進(jìn)行評(píng)估。無(wú)法翻譯 LINQ 表達(dá)式“FirstOrDefault()”并將在本地進(jìn)行評(píng)估。
1 回答

倚天杖
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
問(wèn)題出Func在這里
private Func<int> tenantId => ...
這導(dǎo)致翻譯Invoke(__ef_filter__tenantId_0))和客戶評(píng)價(jià)不佳。
解決方案是制作tenantId簡(jiǎn)單的int返回屬性或方法。例如,保持通話
b.AddTenancy(() => this.tenantId(), "TenantId");
它應(yīng)該改為
private int tenantId()
{
// return tenant id
if (this.tenantContext != null && this.tenantContext.Tenant != null)
{
return this.tenantContext.Tenant.Id;
}
return -1;
};
- 1 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報(bào)
0/150
提交
取消