Entity Framework conditional mapping with code first?

我怕爱的太早我们不能终老 提交于 2019-12-02 11:48:51

I dont think there is an inbuilt method for achieving this, you can however expose a property in your DbContext in which you apply filtering, initially this will be readonly but i dont see a reason why you shouldnt be able to create your own DbSet implementation reflecting back to another DbSet (ProxyDbSet)

Readonly example:

class MyDbContext : DbContext
{
    public IDbSet<User> Users { get; set; }

    public IQueryable<User> Admins 
    {
        get 
        {
            return from user in users
                   where user.Role == "admin"
                   select user;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!