Entity Framework Core , client side computed columns

.net-core c# entity-framework entity-framework-core

Question

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?

1
0
9/12/2017 8:32:46 AM

Accepted Answer

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.

2
9/12/2017 12:48:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow