我使用的是efcore3.1.9和官方的mysql提供程序。我在查询ef core时遇到问题。
这是我的密码:
// Entity mapping configuration
internal class FormGroupConfiguration : EntityTypeConfigurationBase<FormGroup>
{
/// <inheritdoc />
public override void Configure(EntityTypeBuilder<FormGroup> builder)
{
builder.Property(v => v.Id) // Guid
.HasColumnType("varchar(40)");
}
}
// EfRepository is come from a package
public class FormGroupRepository : EfRepository<FormGroup, Guid>, IFormGroupRepository
{
public async Task<FormGroup> GetAsync(Guid id)
{
return await ApplyQueryFilter(Context.Set<FormGroup>())
.SingleOrDefaultAsync(v => v.Id.Equals(id));
}
}
...
// package
public abstract class EfRepository<T, TKey> : IRepository<T, TKey>
where T : class, IAggregateRoot, Domain.Core.Entities.IEntity<TKey>
{
/// <inheritdoc />
public virtual async Task<T> GetByIdAsync(TKey id, CancellationToken cancellationToken = default)
{
return await ApplyQueryFilter(Context.Set<T>())
.SingleOrDefaultAsync(v => v.Id.Equals(id), cancellationToken);
}
}
...
public class SomeService
{
public async Task<FormGroup> DoSomeThing(Guid id)
{
var form1 = await _formGroupRepository.GetByIdAsync(id); // null
var form2 = await _formGroupRepository.GetAsync(id); // not null
}
}
我对不同的查询结果感到困惑。以下是查询日志:
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (3ms) [Parameters=[@__id_0='0x258FCACDB709B046944E64C1A7AC1086' (Nullable = false) (Size = 16)], CommandType='Text', CommandTimeout='30']
SELECT `f`.`Id`, `f`.`ConcurrencyStamp`, `f`.`CreationTime`, `f`.`CreatorId`, `f`.`DeleterId`, `f`.`DeletionTime`, `f`.`IsDeleted`, `f`.`LastModificationTime`, `f`.`LastModifierId`, `f`.`Name`
FROM `FormGroups` AS `f`
WHERE (`f`.`IsDeleted` = FALSE) AND (`f`.`Id` = @__id_0)
LIMIT 2
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (1ms) [Parameters=[@__id_0='cdca8f25-09b7-46b0-944e-64c1a7ac1086' (Nullable = false) (Size = 40)], CommandType='Text', CommandTimeout='30']
SELECT `f`.`Id`, `f`.`ConcurrencyStamp`, `f`.`CreationTime`, `f`.`CreatorId`, `f`.`DeleterId`, `f`.`DeletionTime`, `f`.`IsDeleted`, `f`.`LastModificationTime`, `f`.`LastModifierId`, `f`.`Name`
FROM `FormGroups` AS `f`
WHERE (`f`.`IsDeleted` = FALSE) AND (`f`.`Id` = @__id_0)
LIMIT 2
第一个操作的查询参数似乎变成了“binary(16)”,而第二个操作则不是。当我将数据提供程序切换到'pomelo.entityframeworkcore.mysql'3.2.4时,我发现问题消失了。有人能告诉我原因吗?
暂无答案!
目前还没有任何答案,快来回答吧!