[英]How to determine custom server evaluation in EF core?
I have this entity我有这个实体
public class Foo
{
public int Id{get; private set;}
public DateTime CreatedOn {get; private set;}
public bool IsActive {get; private set;}
public bool CanBeDisplayed => IsActive && CreatedOn > DateTime.Now.AddYear(-1);
}
I want to this dbcontext.Foos.Where(f=>f.CanBeDisplayed).ToArray();
我想要这个
dbcontext.Foos.Where(f=>f.CanBeDisplayed).ToArray();
could be evaluated on the sql server side.可以在 sql 服务器端进行评估。 May there is an attribute or some binding in the DBContext class who can give me a work arround.
可能在 DBContext class 中有一个属性或一些绑定可以给我一个工作。 My goal is to have a Rich Model, ef core compatible, am I missing something?
我的目标是拥有一个 Rich Model,ef 内核兼容,我错过了什么吗?
I see two possible workarounds:我看到两种可能的解决方法:
public class Foo
{
public int Id { get; private set; }
public DateTime CreatedOn { get; private set; }
public bool IsActive { get; private set; }
// mark ignored for EF?
public bool CanBeDisplayed => _compiled(this);
private static Func<Foo, bool> _compiled = CanBeDisplayedExp.Compile();
public static Expression<Func<Foo, bool>> CanBeDisplayedExp = f => f.IsActive && f.CreatedOn > DateTime.Now.AddYears(-1);
}
With usage limited to dbcontext.Foos.Where(Foo.CanBeDisplayedExp).ToArray()
使用仅限于
dbcontext.Foos.Where(Foo.CanBeDisplayedExp).ToArray()
Also not sure that EF can translate DateTime.Now.AddYears
.也不确定 EF 是否可以翻译
DateTime.Now.AddYears
。
PS附言
Just FYI Linq2Db
has expression based calculated colums and Expression Methods support.仅供参考
Linq2Db
具有基于表达式的计算列和表达式方法支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.