简体   繁体   English

使用包含条件的实体代码第一个DbSet

[英]Entity Code First DbSet using include with conditions

I have a class that calls ICB that has a property of type list of ICBResource, that is possible to associate many Resources to one ICB, the problem is when I want to delete a Resource associated with ICB that is not truly deleted and only set true in an deleted boolean property, the resources deleted still return from DB. 我有一个调用ICB的类,该类具有ICBResource类型列表的属性,可以将许多资源关联到一个ICB,问题是当我要删除与ICB关联的资源时,该资源未真正删除,仅设置为true在已删除的布尔属性中,删除的资源仍从数据库返回。

Example: 例:

If this ICB has 10 resources associated but later 3 of resources were deleted, I want to list only the 7 remaining. 如果此ICB有10个关联资源,但后来删除了3个资源,我只想列出剩余的7个资源。

There is a way to implement a conditional for the .includes ? 有一种方法可以为.includes实现条件。

// ICB class
public class ICB
{

    public ICB()
    {
        ICBResources = new List<ICBResource>();
    }

    public int ICBId { get; set; }
    public string Location { get; set; }
    public ICollection<ICBResource> ICBResources { get; set; }

}

// Resource class
public class ICBResource
{
    public int ICBResourceId { get; set; }
    public virtual ICB ICB { get; set; }
    public string name{ get; set; }
    public bool Deleted { get; set; }

}



//here is my Repository and how I select my ICB with the resources associated
public ICB GetICB(int id)
{
    return GetDbSet<ICB>()
            .Include("ICBResources")
            .SingleOrDefault(i => i.ICBId == id);

}

There is a way to include another conditional like a Where(i => i.ICBResources.Where(r=> !r.deleted); 有一种方法可以包含另一个条件,例如Where(i => i.ICBResources.Where(r =>!r.deleted);

for example 例如

public ICB GetICB(int id)
{
    return GetDbSet<ICB>()
        .Include("ICBResources")
        .SingleOrDefault(i => i.ICBId == id && i.ICBResources.Where(r => !r.Deleted).Count() == 0);

}

If the question is about filtering related, the answer is no. 如果问题与过滤有关,那么答案是否定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先使用Entity Framework 4代码在DbContext.DbSet中插入等效的InsertOnSubmit - InsertOnSubmit equivalent in DbContext.DbSet using Entity Framework 4 code first 实体框架代码首先添加 DbSet 属性 - Entity Framework code first adding DbSet property 宣布DBSet <Type> 在DBcontext中 - 实体框架代码优先 - Declaring DBSet<Type> within DBcontext - Entity Framework Code First Code-First:将实体添加到dbset时的空引用异常 - Code-First: Null reference exception when adding an entity to a dbset 实体框架6模拟包括dbset上的方法 - Entity framework 6 mocking include method on dbset 没有代理的实体框架DBSet包含 - Entity Framework DBSet Include without proxies 当在DbSet中使用Include时,C#实体框架返回损坏的JSon - C# Entity Framework returning broken JSon when using Include in DbSet 添加到包含列表的类的dbset时,实体框架代码首先出现NullReferenceException - Entity Framework Code First NullReferenceException when adding to a dbset of class that contains a list 如何使用Code First Entity Framework在C#中包括可选关系 - How to include optional relations in C# using Code First Entity Framework 实体框架4.1代码优先 - 使用LinqKit PredicateBuilder时忽略Include - Entity Framework 4.1 Code First - Include is being ignored when using LinqKit PredicateBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM