简体   繁体   English

实体框架,带有where条件的相关实体的常规加载

[英]Entity Framework, Generic loading of related entities with a where condition

I'm trying to create a function that generically loads the related child entities with a filter. 我正在尝试创建一个函数,该函数通常使用过滤器加载相关的子实体。

All my entities are derived from my own Base Class "BusinessObject" 我所有的实体都来自我自己的基类“ BusinessObject”

public abstract class BusinessObject : BaseObject, IBaseObject, ILocalObject
{
    [Browsable(false)]
    [Key]
    public int ID { get; set; }

    [Browsable(false)]
    public int? HqID { get; set; }

    private bool _deleted;
    [Browsable(false)]
    public bool Deleted
    {
        get { return _deleted; }
        set { CheckPropertyChanged(ref _deleted,  value); }
    }
}

I have created the following function that when supplied an entity will load all the related child objects. 我创建了以下函数,当提供一个实体时,它将加载所有相关的子对象。 When defining my entities, all child collections are flagged by my own attribute "EntityChildCollectionAttribute" so I can easily find the collections I want to load. 定义实体时,所有子集合都由我自己的属性“ EntityChildCollectionAttribute”标记,因此我可以轻松找到要加载的集合。

    public virtual void OnLoadEntityChildren(object entity)
    {
        var propNames = entity.GetPropertyNames();
        foreach (var propName in propNames.Where(propName => entity.PropertyHasCustomAttribute(propName, typeof(EntityChildCollectionAttribute))))
        {
            MyData.Entry(entity).Collection(propName).Load();                               
        }
    }

This works lovely! 这很可爱! My Problem comes when I want to filter the child collection. 当我想过滤子集合时,我的问题来了。

In this case I want to only load child entities where Deleted == false. 在这种情况下,我只想加载Deleted == false的子实体。

I cannot work out how to do this! 我不知道该怎么做!

I have had many attempts and replacing MyData.Entry(entity).Collection(propName).Load(); 我已经尝试了很多次,并替换了MyData.Entry(entity).Collection(propName).Load(); with

MyData.Entry(entity).Collection(propName).Query().Cast<BusinessObject>().Where(x=>x.Deleted.Equals(false)).Load();

compiles but then I get the error; 编译但是我得到了错误;

"Unable to cast the type 'FmOrderProcessing.Entities.OpDocumentDetail' to type 'FwBaseEntityFramework.BusinessObject'. LINQ to Entities only supports casting EDM primitive or enumeration types." “无法将类型'FmOrderProcessing.Entities.OpDocumentDetail'强制转换为'FwBaseEntityFramework.BusinessObject'。LINQto Entities仅支持强制转换EDM基本类型或枚举类型。”

Any Help/Pointers/Answers will be gratefully received 任何帮助/指针/答案将不胜感激

Thanks in advance 提前致谢

Lance 长矛

I was implementing a "Soft Delete" pattern which means the records in the database are flagged as deleted rather than removed (for audit and replication purposes). 我正在实现“软删除”模式,这意味着将数据库中的记录标记为已删除而不是删除(出于审计和复制目的)。

All entities are derived from a base definition with a bool Deleted property. 所有实体均来自具有bool Deleted属性的基本定义。

I found the answer here: 我在这里找到了答案:

https://www.nuget.org/packages/EntityFramework.DynamicFilters https://www.nuget.org/packages/EntityFramework.DynamicFilters

This package allows the definition of global filters in the data context. 该软件包允许在数据上下文中定义全局过滤器。 I fixed my issue with one line of code in the OnModelCreating override. 我用OnModelCreating覆盖中的一行代码解决了我的问题。

modelBuilder.Filter("Deleted", (IBaseObject d) =>d.Deleted, false);

The filter function is applied globally to any entity presenting (in my case) the IBaseObject interface. 筛选器函数全局应用于所有呈现(在我的情况下)IBaseObject接口的实体。

I hope this helps any body else with a similar issue 我希望这对其他有类似问题的人有帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM