DbSet.Find() 不适用于 Guid 属性



我有两个模型

public class QChoice : ModelWithGuid
{
[Required]
public Guid? QId { get; set; }
}
public class ModelWithGuid
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
}

我正在 CHOICE Dbset 上执行 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;
}

我收到类似"调用'DbSet.Find'的位置 0 的键值是'表达式>'类型,这与'Guid'的属性类型不匹配"之类的错误。请帮助我

根据这个文档,

EF CoreDbSet<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;
}

相关内容

最新更新