简体   繁体   中英

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

  2. x => x.TabAccountLangs -> should take the property I specify on calling the Method (maybe threw MemberExpression)

  3. from x ...TabAccounts -> should load the DbSet from the EntitySet I am calling the method with

  4. from y in x.TabAccountLangs -> should be the property from above

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.

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? 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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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