检索DBSET< t>的通用方法来自dbContext和使用Incluph()方法C#

发布于 2025-01-31 16:40:05 字数 805 浏览 4 评论 0原文

我制作了此功能来检索所有数据。

public static List<Volunteer> GetAllVolunteers()
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.Volunteers.**Include(v => v.roleForVolunteers).Include(v => v.VolunteerOffers)
                         .Include("VolunteerOffers.daysForAVolunteers")**.ToList();
    }
}

而且我具有检索DBSET T的通用功能。

public static List<T> GetDbSet<T>() where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.GetDbSet<T>().ToList();
    }
}

我可以制作一个通用函数,以检索连接到一个DBSET T的所有数据吗?

提前致谢。

I made this function that retrieves all the data.

public static List<Volunteer> GetAllVolunteers()
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.Volunteers.**Include(v => v.roleForVolunteers).Include(v => v.VolunteerOffers)
                         .Include("VolunteerOffers.daysForAVolunteers")**.ToList();
    }
}

And I have this generic function that retrieves DBSet T.

public static List<T> GetDbSet<T>() where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.GetDbSet<T>().ToList();
    }
}

Can I make a generic function that will retrieve all the data that connect to one DBSet T?

Thanks in advance.

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

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

发布评论

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

评论(3

无可置疑 2025-02-07 16:40:05

获取dbset t从上下文中的实体类类型,例如dbset&lt; t&gt;iqueryable&lt; t&gt;

在此上> etc,在命名空间system.linq的帮助下。

  • 使用

    include()方法,传递所需的属性。

  • 用于并行处理,使用system.threading.tasks

    的TPL

尝试以下操作:

public static List<T> GetDbSet<T>(string[] includeProperties) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        Parallel.ForEach(inlcudeProperties, includeProperty => {
           query.Include(includeProperty);
        });

        return query.ToList();
    }
}

Get DbSet for the T Entity Class Type from context as DbSet<T> or IQueryable<T>.

On this IQueryable<T> object can perform multiple operations like Where(), Include(), orderBy() etc with help of namespace System.Linq.

  • With Include() method, pass on required included properties.

  • For Parallel processing use TPL from System.Threading.Tasks

Try this:

public static List<T> GetDbSet<T>(string[] includeProperties) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        Parallel.ForEach(inlcudeProperties, includeProperty => {
           query.Include(includeProperty);
        });

        return query.ToList();
    }
}
惜醉颜 2025-02-07 16:40:05

大个月前,我尝试这样做这样的事情,但是我有tablename。我给您一些细节,可以使

您的提取提取曲目上下文,并仅选择t类型的clrtype corrispond。

var selectedTable = contextDb.Model
            .GetEntityTypes()
            .Select(x => new TableSpecification // Custom class
            {
                TableName = x.GetTableName(),
                ClrType = x.ClrType,
                JoinTable = x.GetForeignKeys(),
                JoinTableProp = x.GetForeignKeyProperties(),
                NavigationProp = x.GetNavigations(),
                ColumnsTable = null
            })
           .Where(// where ClrType)
           .Distinct()
           .FirstOrDefault();

这是我的自定义课程

using Microsoft.EntityFrameworkCore.Metadata;

using System;
using System.Collections.Generic;

namespace Seac.CRM.DbInfrastructure.Model.Context;

public class TableSpecification
{
    public string TableName { get; init; }
    public Type ClrType { get; init; }
    public IEnumerable<IForeignKey> JoinTable { get; init; }
    public IEnumerable<IProperty> JoinTableProp { get; init; }
    public IEnumerable<INavigation> NavigationProp { get; init; }
    public IEnumerable<IProperty> ColumnsTable { get; init; }
}

的所有参考属性

var joinLookup = selectedTable.JoinTable.GetLookupForeignKey();

foreach (var lookup in joinLookup)
   queryable = queryable.Include(lookup.PrincipalEntityType.Name.Split(".").Last());

,您可以提取我希望可以帮助您

。 *** 编辑

如果您需要自定义,则可以使用iincludableQueryable&lt; t,对象&gt;&gt;

public static List<T> GetDbSet<T>(IIncludableQueryable<T, object>> include = null) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        if (includesFunc is not null)
            query = includesFunc(query);

        return query.ToList();
    }
}

Some month ago i try to do somthing like that, but i have tableName. I leave you some detail, that you can adapt

You extract form context all your table and select only the ClrType corrispond with your T type.

var selectedTable = contextDb.Model
            .GetEntityTypes()
            .Select(x => new TableSpecification // Custom class
            {
                TableName = x.GetTableName(),
                ClrType = x.ClrType,
                JoinTable = x.GetForeignKeys(),
                JoinTableProp = x.GetForeignKeyProperties(),
                NavigationProp = x.GetNavigations(),
                ColumnsTable = null
            })
           .Where(// where ClrType)
           .Distinct()
           .FirstOrDefault();

This is my custom class

using Microsoft.EntityFrameworkCore.Metadata;

using System;
using System.Collections.Generic;

namespace Seac.CRM.DbInfrastructure.Model.Context;

public class TableSpecification
{
    public string TableName { get; init; }
    public Type ClrType { get; init; }
    public IEnumerable<IForeignKey> JoinTable { get; init; }
    public IEnumerable<IProperty> JoinTableProp { get; init; }
    public IEnumerable<INavigation> NavigationProp { get; init; }
    public IEnumerable<IProperty> ColumnsTable { get; init; }
}

then you can extract all your reference property

var joinLookup = selectedTable.JoinTable.GetLookupForeignKey();

foreach (var lookup in joinLookup)
   queryable = queryable.Include(lookup.PrincipalEntityType.Name.Split(".").Last());

I wish that can help you.

*** EDIT

If you need a custom include of T you can use IIncludableQueryable<T, object>>

public static List<T> GetDbSet<T>(IIncludableQueryable<T, object>> include = null) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        if (includesFunc is not null)
            query = includesFunc(query);

        return query.ToList();
    }
}
我纯我任性 2025-02-07 16:40:05

感谢所有试图帮助我的人。
这是对我有用的解决方案。

  public static List<T> GetDbSetWithIncludes<T>(string[] includes) where T : class
    {
        using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
        {
            db.Configuration.LazyLoadingEnabled = false;
            return IncludeMultiple<T>(db.GetDbSet<T>(), includes).ToList();
        }
    }


 public static IQueryable<T> IncludeMultiple<T>(IQueryable<T> query, string[] includes)where T : class
        {
            if (includes != null)
            {
                query = includes.Aggregate(query, (current, include) =>
                current.Include(include));
            }
            return query;
        }

Thanks to everyone who tried to help me.
This is the solution that worked for me.

  public static List<T> GetDbSetWithIncludes<T>(string[] includes) where T : class
    {
        using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
        {
            db.Configuration.LazyLoadingEnabled = false;
            return IncludeMultiple<T>(db.GetDbSet<T>(), includes).ToList();
        }
    }


 public static IQueryable<T> IncludeMultiple<T>(IQueryable<T> query, string[] includes)where T : class
        {
            if (includes != null)
            {
                query = includes.Aggregate(query, (current, include) =>
                current.Include(include));
            }
            return query;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文