实体框架的代码第一个条件映射? [英] Entity Framework conditional mapping with code first?

查看:108
本文介绍了实体框架的代码第一个条件映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要返回只记录该商品的特定字段值大于零的实体。我见过有条件映射的例子在EDMX,而且好像我需要什么。然而,我的项目是EF 4.1代码第一位。
请问有没有办法做到这一点使用代码优先的方法?

I have an entity that I need to return only records where a given field value is greater than zero. I have seen examples of conditional mapping in the edmx and that seems like what I am in need of. However, my project is in EF 4.1 code first. Is there not a way to do this using the code first approach?

推荐答案

我不认为有一个内置的为实现这个方法,但是,您可以暴露你的DbContext在其中应用过滤属性,最初这将是只读的,但我不明白了一个道理,为什么你不应该能够创建自己的DbSet实现反射回另一个DbSet(ProxyDbSet)

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)

只读例如:

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

这篇关于实体框架的代码第一个条件映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆