简体   繁体   中英

Entity Framework Core , client side computed columns

I am trying to find a way to have a client side computed column in an easy fashion. There already is the possibility to have computed columns on the server via : https://docs.microsoft.com/en-us/ef/core/modeling/relational/computed-columns

what I would like instead is something like this:

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

Is it possible to do it with EF core?

If it's only a client-side column, then you don't need to use anything from Entity Framework. I would suggest implementing custom property getter in you Person class, like this:

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];
            }
        }
    }
}

I've assumed that DeptName property type is string , but obviously you should change it to match your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM