在 Efcore 中包含子过滤器未按预期工作
In Efcore Include with filter by child not working as expected
我正在使用 EFCore 5 并且我遵循结构
public class User: AuditableEntity
{
public string Id { get; set; }
public string Discriminator { get; set; }
public User()
{
}
}
public class CustomerUser: User
{
public string CustomerId { get; set; }
// public bool? IsAdmin { get; set; }
public Customer Customer { get; set; }
}
public class CustomerAdmin: CustomerUser
{
}
public class Customer: AuditableEntity
{
...
public ICollection<CustomerUser> CustomerUsers { get; private set; }
...
}
我正在使用 LINQ 查询来获取基于子类类型的关系。有人认为我必须使用鉴别器列。
await _dbContext.Customers
.Include(x => x.CustomerUsers.Where(u => u is CustomerAdmin))
.Select(x => new TmpQueryResult
{
CustomerId = x.Id,
UserInfo = x.CustomerUsers.Select(y => new UserInfo
{
Id = y.Id,
IsAdmin = y is CustomerAdmin
})
})
.FirstOrDefaultAsync(x => x.CustomerId == request.CustomerId);;
生成的查询是
Executed DbCommand (30ms) [Parameters=[@__request_CustomerId_0='16d2d324-0aac-4c9e-af3a-fef537a4de70' (Size = 255)], CommandType='Text', CommandTimeout='30']
SELECT `t`.`Id`, `t0`.`Id`, `t0`.`IsAdmin`
FROM (
SELECT `c`.`Id`
FROM `Customers` AS `c`
WHERE `c`.`Id` = @__request_CustomerId_0
LIMIT 1
) AS `t`
LEFT JOIN (
SELECT `u`.`Id`, `u`.`Discriminator` = 'CustomerAdmin' AS `IsAdmin`, `u`.`CustomerId`
FROM `Users` AS `u`
WHERE `u`.`Discriminator` IN ('CustomerUser', 'CustomerAdmin', 'OfficeManager', 'Provider')
) AS `t0` ON `t`.`Id` = `t0`.`CustomerId`
ORDER BY `t`.`Id`, `t0`.`Id`
u => u is CustomerAdmin
没有被翻译成
鉴别器` IN ('CustomerAdmin')
我正在使用 Pomelo.EntityFrameworkCore.MySql 版本 5.0.0。我不明白为什么它没有按预期工作,或者我在这里做错了什么。
对于自定义投影 Include
被完全忽略,所以你必须在投影中重复你的鉴别器过滤器。
await _dbContext.Customers
.Select(x => new TmpQueryResult
{
CustomerId = x.Id,
UserInfo = x.CustomerUsers
.Where(u => u is CustomerAdmin)
.Select(y => new UserInfo
{
Id = y.Id,
IsAdmin = y is CustomerAdmin
})
})
.FirstOrDefaultAsync(x => x.CustomerId == request.CustomerId);;
我正在使用 EFCore 5 并且我遵循结构
public class User: AuditableEntity
{
public string Id { get; set; }
public string Discriminator { get; set; }
public User()
{
}
}
public class CustomerUser: User
{
public string CustomerId { get; set; }
// public bool? IsAdmin { get; set; }
public Customer Customer { get; set; }
}
public class CustomerAdmin: CustomerUser
{
}
public class Customer: AuditableEntity
{
...
public ICollection<CustomerUser> CustomerUsers { get; private set; }
...
}
我正在使用 LINQ 查询来获取基于子类类型的关系。有人认为我必须使用鉴别器列。
await _dbContext.Customers
.Include(x => x.CustomerUsers.Where(u => u is CustomerAdmin))
.Select(x => new TmpQueryResult
{
CustomerId = x.Id,
UserInfo = x.CustomerUsers.Select(y => new UserInfo
{
Id = y.Id,
IsAdmin = y is CustomerAdmin
})
})
.FirstOrDefaultAsync(x => x.CustomerId == request.CustomerId);;
生成的查询是
Executed DbCommand (30ms) [Parameters=[@__request_CustomerId_0='16d2d324-0aac-4c9e-af3a-fef537a4de70' (Size = 255)], CommandType='Text', CommandTimeout='30']
SELECT `t`.`Id`, `t0`.`Id`, `t0`.`IsAdmin`
FROM (
SELECT `c`.`Id`
FROM `Customers` AS `c`
WHERE `c`.`Id` = @__request_CustomerId_0
LIMIT 1
) AS `t`
LEFT JOIN (
SELECT `u`.`Id`, `u`.`Discriminator` = 'CustomerAdmin' AS `IsAdmin`, `u`.`CustomerId`
FROM `Users` AS `u`
WHERE `u`.`Discriminator` IN ('CustomerUser', 'CustomerAdmin', 'OfficeManager', 'Provider')
) AS `t0` ON `t`.`Id` = `t0`.`CustomerId`
ORDER BY `t`.`Id`, `t0`.`Id`
u => u is CustomerAdmin
没有被翻译成
鉴别器` IN ('CustomerAdmin')
我正在使用 Pomelo.EntityFrameworkCore.MySql 版本 5.0.0。我不明白为什么它没有按预期工作,或者我在这里做错了什么。
对于自定义投影 Include
被完全忽略,所以你必须在投影中重复你的鉴别器过滤器。
await _dbContext.Customers
.Select(x => new TmpQueryResult
{
CustomerId = x.Id,
UserInfo = x.CustomerUsers
.Where(u => u is CustomerAdmin)
.Select(y => new UserInfo
{
Id = y.Id,
IsAdmin = y is CustomerAdmin
})
})
.FirstOrDefaultAsync(x => x.CustomerId == request.CustomerId);;