Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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# 实体框架核心,客户端计算列_C#_Entity Framework_.net Core_Entity Framework Core - Fatal编程技术网

C# 实体框架核心,客户端计算列

C# 实体框架核心,客户端计算列,c#,entity-framework,.net-core,entity-framework-core,C#,Entity Framework,.net Core,Entity Framework Core,我试图找到一种方法,以一种简单的方式创建客户端计算列。 已经可以通过以下方式在服务器上计算列: 我想要的是这样的: modelBuilder.Entity<Person>() .Property(p => p.DeptName) .HasClientComputedColumn( (context, entity) =>{ return myStaticMap[entity.Id]; }); cl

我试图找到一种方法,以一种简单的方式创建客户端计算列。 已经可以通过以下方式在服务器上计算列:

我想要的是这样的:

modelBuilder.Entity<Person>()
 .Property(p => p.DeptName)
 .HasClientComputedColumn( (context, entity) =>{        
            return myStaticMap[entity.Id];      
        });
class Person
{
    //other fields...

    public string DeptName
    {
        get
        {
            if(myStaticMap==null || !myStaticMap.Contains(this.Id))
            {
                //initialize your static map or throw exception
            }
            else
            {
                return myStaticMap[this.Id];
            }
        }
    }
}
modelBuilder.Entity()
.Property(p=>p.DeptName)
.HasClientComputedColumn((上下文,实体)=>{
返回myStaticMap[entity.Id];
});

是否可以使用EF core进行此操作?

如果它只是一个客户端列,则不需要使用Entity Framework中的任何内容。我建议在
Person
类中实现自定义属性getter,如下所示:

modelBuilder.Entity<Person>()
 .Property(p => p.DeptName)
 .HasClientComputedColumn( (context, entity) =>{        
            return myStaticMap[entity.Id];      
        });
class Person
{
    //other fields...

    public string DeptName
    {
        get
        {
            if(myStaticMap==null || !myStaticMap.Contains(this.Id))
            {
                //initialize your static map or throw exception
            }
            else
            {
                return myStaticMap[this.Id];
            }
        }
    }
}

我假设
DeptName
属性类型是
string
,但显然您应该更改它以满足您的需要。

如果它只是一个客户端列,那么您不需要使用实体框架中的任何内容。我建议在
Person
类中实现自定义属性getter,如下所示:

modelBuilder.Entity<Person>()
 .Property(p => p.DeptName)
 .HasClientComputedColumn( (context, entity) =>{        
            return myStaticMap[entity.Id];      
        });
class Person
{
    //other fields...

    public string DeptName
    {
        get
        {
            if(myStaticMap==null || !myStaticMap.Contains(this.Id))
            {
                //initialize your static map or throw exception
            }
            else
            {
                return myStaticMap[this.Id];
            }
        }
    }
}

我假设
DeptName
属性类型是
string
,但显然您应该更改它以满足您的需要。

如果是客户端,为什么不使用自定义C#property getter?@MarcinZablocki好吧,我简化了一点问题,并将解决方案简化为您所说的那样(尽管静态映射很糟糕)。事实上,我想要定义一个硬编码的
DBSet
,它可以被EF查询,即使它的值在数据库中不存在。这可以做到吗?@Marcinzabloki你可以回答这个问题,我会接受你的回答(也许你可以消除对
静态
依赖关系的需求?)。我正在寻找一种从不同数据源解析数据的GraphQL风格,请详细说明“也许您可以消除对静态依赖关系的需要”?@MarcinZablocki我不是EF bug方面的专家删除静态依赖项意味着实体有一些与实体框架分离的应用程序数据,可以从实体内部访问这些数据。。。所以,如果我们想保持它的简单,getter中的静态引用是一种方法。但是,您必须确保在访问之前对引用的静态映射进行初始化……如果是客户端,为什么不使用自定义C#property getter?@MarcinZablocki嗯,我简化了一点问题,并将解决方案简化为您所说的那样(即使静态映射很糟糕)。事实上,我想要定义一个硬编码的
DBSet
,它可以被EF查询,即使它的值在数据库中不存在。这可以做到吗?@Marcinzabloki你可以回答这个问题,我会接受你的回答(也许你可以消除对
静态
依赖关系的需求?)。我正在寻找一种从不同数据源解析数据的GraphQL风格,请详细说明“也许您可以消除对静态依赖关系的需要”?@MarcinZablocki我不是EF bug方面的专家删除静态依赖项意味着实体有一些与实体框架分离的应用程序数据,可以从实体内部访问这些数据。。。所以,如果我们想保持它的简单,getter中的静态引用是一种方法。但是,在访问之前,您必须确保引用的静态映射已初始化。。。。