简体   繁体   English

EF 5.0-加载T的集合

[英]EF 5.0 - Loading collections of T

I would like to write the following code more abstract. 我想将下面的代码写得更加抽象。

private void LoadRelatedData(TabAccount tabAccount)
{
    if (ConxCore.Instance.EntityModel.Entry(tabAccount).Collection(x => x.TabAccountLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabAccounts
            from y in x.TabAccountLangs
            select y).ToList();
    }
}

The parts I would like to abstract are the following: 我想抽象的部分如下:

  1. .Entry(tabAccount) -> should take every EntitySet .Entry(tabAccount) ->应该接受每个EntitySet

  2. x => x.TabAccountLangs -> should take the property I specify on calling the Method (maybe threw MemberExpression) x => x.TabAccountLangs- >应该采用我在调用Method时指定的属性(也许抛出MemberExpression)

  3. from x ...TabAccounts -> should load the DbSet from the EntitySet I am calling the method with 从x ... TabAccounts- >应该从EntitySet加载DbSet我正在使用

  4. from y in x.TabAccountLangs -> should be the property from above 从x.TabAccountLangs中的y- >应该是上面的属性

About the abstraction, I just want to avoid copy/pasting this code over and over and just changing the 4 mentioned points, would be happy to be able to call this method with paramaters given and the method does the rest. 关于抽象,我只是想避免一遍又一遍地复制/粘贴此代码,而只是更改提到的4点,很高兴能够在给定参数的情况下调用此方法,而该方法完成了其余工作。

So instead of: 所以代替:

private void LoadRelatedData(TabAccount tabAccount)
{
    if (ConxCore.Instance.EntityModel.Entry(tabAccount).Collection(x => x.TabAccountLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabAccounts
            from y in x.TabAccountLangs
            select y).ToList();
    }
}

private void LoadRelatedData(TabElement tabElement)
{
    if (ConxCore.Instance.EntityModel.Entry(tabElement).Collection(x => x.TabElementLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabElements
            from y in x.TabElementLangs
            select y).ToList();
    }
}

Something like this (only pseudo code): 像这样(仅伪代码):

private void LoadRelatedData(object obj, object collection, object dbSetOfObj)
{
    if (ConxCore.Instance.EntityModel.Entry(obj).Collection(x => x.collection).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.dbSetOfObj
            from y in x.collection
            select y).ToList();
    }
}

And call this method: 并调用此方法:

LoadRelatedData(tabAccount, TabAccountLangs, TabAccounts);
LoadRelatedData(tabElement, TabElementLangs, TabElements);

Hope you can help me out. 希望你能帮助我。 Thanks in advance. 提前致谢。

using System.Data.Entity; // For the Include extension method.

private void LoadRelatedData<TEntity, TRelated>(TEntity entity, Expression<Func<TEntity, ICollection<TRelated>> navigationProperty)
    where TEntity : class
    where TRelated : class
{
    if (!ConxCore.Instance.EntityModel.Entry(entity).Collection(navigationProperty).IsLoaded) 
    {
        var list = ConxCore.Instance.EntityModel.Set<TEntity>().Include(navigationProperty).ToList();
    }
}

And then, you can call it by writing: 然后,您可以通过编写以下内容来调用它:

LoadRelatedData(tabAccount, ta => ta.TabAccountLangs);
LoadRelatedData(tabElement, te => te.TabElementLangs);

However, you know that such a call will load all of the related data, for any x? 但是,您知道这样的调用将加载所有相关数据,对于任何x? That is, you may load a considerable amount of data in a not particularly optimized way. 也就是说,您可能会以非特别优化的方式加载大量数据。 If you only want to load the related data of a single entity, you can load the collection from the object returned by the Collection call: 如果只想加载单个实体的相关数据,则可以从Collection调用返回的对象中加载集合:

private void LoadRelatedDataOfSingleObject<TEntity, TRelated>(TEntity entity, Expression<Func<TEntity, ICollection<TRelated>> navigationProperty)
    where TEntity : class
    where TRelated : class
{
    var collectionEntry = ConxCore.Instance.EntityModel.Entry(entity).Collection(navigationProperty);
    if (!collectionEntry.IsLoaded) collectionEntry.Load();
}

However, if you want to load the data for many objects, you should consider using one of the Include extension methods . 但是,如果要加载许多对象的数据,则应考虑使用Include扩展方法之一

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

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