[英]Generic method to retrieve DbSet <T> from DbContext and Using Include() method c#
我做了這個 function 來檢索所有數據。
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();
}
}
我有這個通用的 function 可以檢索 DBSet T。
public static List<T> GetDbSet<T>() where T : class
{
using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
{
return db.GetDbSet<T>().ToList();
}
}
我可以制作一個通用的 function 來檢索連接到一個 DBSet T 的所有數據嗎?
提前致謝。
獲取T
實體的DbSet
Class 從上下文中鍵入DbSet<T>
或IQueryable<T>
。
在這個IQueryable<T>
object 上,可以在命名空間System.Linq
的幫助下執行多個操作,如Where()
、 Include()
、 orderBy()
等。
使用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();
}
}
一個月前,我嘗試做類似的事情,但我有 tableName。 我留給你一些細節,你可以適應
您提取表格上下文所有表和 select 只有 ClrType 與您的 T 類型相對應。
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();
這是我的習慣 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; }
}
然后您可以提取所有參考屬性
var joinLookup = selectedTable.JoinTable.GetLookupForeignKey();
foreach (var lookup in joinLookup)
queryable = queryable.Include(lookup.PrincipalEntityType.Name.Split(".").Last());
我希望這可以幫助你。
***編輯
如果你需要自定義包含 T 你可以使用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();
}
}
感謝所有試圖幫助我的人。 這是對我有用的解決方案。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.