DbSet.Find() 不适用于 Guid 属性
DbSet.Find() doesn't work for Guid properties
我有两个模型
public class QChoice : ModelWithGuid
{
[Required]
public Guid? QId { get; set; }
}
public class ModelWithGuid
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
}
我正在对 CHOICE 数据库集执行 FInd 操作。
DataContext.QChoice.Find(choice => choice.QId.Value.Equals("Guid")).ToList();
从上面的行查找操作调用下面的方法。
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
var resultData = table.Find(predicate);//table means QChoice DbSet
yield return resultData;
}
我遇到类似 "The key value at position 0 of the call to 'DbSet.Find' was of type 'Expression>', which does not match the property type of 'Guid'" 的错误。请帮助我
根据 this 文档,
EF Core DbSet<TEntity>.Find
不采用任何 predicate
而是采用实体的主键作为对象,如下所示:
var qChoice = DataContext.QChoice.Find(choice.QId);
此外,如果您想使用谓词进行查找而不是按如下方式更新您的 Find
方法:
public T Find(Expression<Func<T, bool>> predicate)
{
var resultData = table.Where(predicate).FirstOrDefault();
return resultData;
}
我有两个模型
public class QChoice : ModelWithGuid
{
[Required]
public Guid? QId { get; set; }
}
public class ModelWithGuid
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
}
我正在对 CHOICE 数据库集执行 FInd 操作。
DataContext.QChoice.Find(choice => choice.QId.Value.Equals("Guid")).ToList();
从上面的行查找操作调用下面的方法。
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
var resultData = table.Find(predicate);//table means QChoice DbSet
yield return resultData;
}
我遇到类似 "The key value at position 0 of the call to 'DbSet.Find' was of type 'Expression>', which does not match the property type of 'Guid'" 的错误。请帮助我
根据 this 文档,
EF Core DbSet<TEntity>.Find
不采用任何 predicate
而是采用实体的主键作为对象,如下所示:
var qChoice = DataContext.QChoice.Find(choice.QId);
此外,如果您想使用谓词进行查找而不是按如下方式更新您的 Find
方法:
public T Find(Expression<Func<T, bool>> predicate)
{
var resultData = table.Where(predicate).FirstOrDefault();
return resultData;
}