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

时间:2017-09-12 08:32:46

标签: c# entity-framework .net-core entity-framework-core

我正在尝试以简单的方式找到一种方法来建立客户端计算列。 已经有可能通过以下方式在服务器上计算列: https://docs.microsoft.com/en-us/ef/core/modeling/relational/computed-columns

我想要的是这样的事情:

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

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

1 个答案:

答案 0 :(得分:2)

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

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,但显然您应该根据自己的需要进行更改。