告诉 EF 不要考虑位字段 = 1 (isDeleted) 的记录

发布于 2024-10-19 04:35:15 字数 170 浏览 0 评论 0原文

所有表都有一个位字段 IsDeleted

有没有办法告诉 EF 在执行查询时不要考虑它们。

或者我只需每次都指定一下 Where(o => o.IsDeleted != true)

(首先使用 EF4 CTP5 代码)

all tables have a bit field IsDeleted

is there a way to tell EF not to consider them when doing queries.

or I just have to specify this each time like Where(o => o.IsDeleted != true)

(using EF4 CTP5 code first)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-10-26 04:35:15

我通过在通用存储库中执行此操作解决了这个问题,我还为没有 isDeleted 的实体创建了一个只读存储库(它们根本不由应用程序管理,只是读取),只读存储库获取所有

记录simple repo 继承了 readonly 并重写了不应该返回标有 isDeleted = false 的实体的方法;

应该标记为 IsDeleted = true 的实体从 DelEntity 继承

public class DelEntity : Entity, IDel
{
    public bool IsDeleted { get; set; }
}

我的通用存储库:

public class Repo<T> : ReadRepo<T>, IRepo<T> where T : DelEntity, new()
    {
        public Repo(IDbContextFactory a) : base(a)
        {
        }

...
        public override IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return c.Set<T>().Where(predicate).Where(o => o.IsDeleted == false);
        }

        public override IEnumerable<T> GetAll()
        {
            return c.Set<T>().Where(o => o.IsDeleted == false);
        }
    }

我的只读操作存储库

public class ReadRepo<T> : IReadRepo<T> where T : Entity, new()
    {
        protected readonly DbContext c;

        public ReadRepo(IDbContextFactory f)
        {
            c = f.GetContext();
        }

        public T Get(long id)
        {
            return c.Set<T>().Find(id);
        }

        public virtual IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return c.Set<T>().Where(predicate);
        }

        public virtual IEnumerable<T> GetAll()
        {
            return c.Set<T>();
        }

        public int Count()
        {
            return c.Set<T>().Count();
        }

    }

此解决方案有点特定于我的情况,但希望您可以从中得到一些想法

I solved this by doing this in my generic repository, I also made a readonly repository for entities that don't have the isDeleted (they aren't managed by the app at all, just reading), the readonly repo gets all the records

the simple repo inherits the readonly one and overrides the methods that should not return entities marked with isDeleted = false;

entities that should be just marked IsDeleted = true inherit from DelEntity

public class DelEntity : Entity, IDel
{
    public bool IsDeleted { get; set; }
}

my generic repository:

public class Repo<T> : ReadRepo<T>, IRepo<T> where T : DelEntity, new()
    {
        public Repo(IDbContextFactory a) : base(a)
        {
        }

...
        public override IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return c.Set<T>().Where(predicate).Where(o => o.IsDeleted == false);
        }

        public override IEnumerable<T> GetAll()
        {
            return c.Set<T>().Where(o => o.IsDeleted == false);
        }
    }

my repository for readonly operations

public class ReadRepo<T> : IReadRepo<T> where T : Entity, new()
    {
        protected readonly DbContext c;

        public ReadRepo(IDbContextFactory f)
        {
            c = f.GetContext();
        }

        public T Get(long id)
        {
            return c.Set<T>().Find(id);
        }

        public virtual IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return c.Set<T>().Where(predicate);
        }

        public virtual IEnumerable<T> GetAll()
        {
            return c.Set<T>();
        }

        public int Count()
        {
            return c.Set<T>().Count();
        }

    }

this solution it's a bit specific to my case, but hopefully you might get some ideas from it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文