Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确定EF core中的自定义服务器评估?_C#_Entity Framework_Domain Driven Design_Ef Core 3.1 - Fatal编程技术网

C# 如何确定EF core中的自定义服务器评估?

C# 如何确定EF core中的自定义服务器评估?,c#,entity-framework,domain-driven-design,ef-core-3.1,C#,Entity Framework,Domain Driven Design,Ef Core 3.1,我有这个实体 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

我有这个实体

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);
}
我想将这个
dbcontext.Foos.Where(f=>f.CanBeDisplayed.ToArray()。DBContext类中可能有一个属性或一些绑定,可以为我提供一个工作环境。

我的目标是有一个丰富的模式,ef核心兼容,我错过了什么

我看到两种可能的解决方法:

  • 介绍并为其提供sql
  • 制作表达式树
  • 公共类Foo
    {
    public int Id{get;private set;}
    public DateTime CreatedOn{get;private set;}
    public bool IsActive{get;private set;}
    //为EF忽略标记?
    公共布尔值可显示=>\u已编译(此);
    private static funcu compiled=CanBeDisplayedExp.Compile();
    公共静态表达式CanBeDisplayedExp=f=>f.IsActive&&f.CreatedOn>DateTime.Now.AddYears(-1);
    }
    
    用法仅限于
    dbcontext.Foos.Where(Foo.CanBeDisplayedExp.ToArray()

    也不确定EF是否可以翻译
    DateTime.Now.AddYears

    附言

    仅供参考
    Linq2Db
    拥有和支持

    请参见生成的值:
    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);
    }