Net core和efcore db优先使用的方法。我有两张桌子。第一个表是SiteDetails,它包含与Sitedetails相关的列,并且它基于主键和外键关系引用其他表国家/地区。现在,我也想将这些国家也包括在内。下面是我的通用方法。
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
在下面的代码中,我正在调用上面的方法
var siteDetails= await this.siteDetailsRepository.GetAsync(x => x.siteNo== siteNo , source => source.Include(???)).ConfigureAwait(false);
下面是siteDetails页面国家表之间的关系SitDetails具有字段siteNo,siteName,CountryId国家的字段为CountryId和CountryName
[有人可以帮我在这里写包含语法吗?任何帮助,将不胜感激。谢谢
[要使用GenericRepository时,应在初始化时声明Type。如果您不使用显式类型(如SiteDetail)初始化GenericRepository,则在您的示例中正在初始化:
public class SiteDetailService : ISiteService
{
private readonly IBaseRepository<SiteDetail> _siteDetailRepository;
public SiteDetailService(IBaseRepository<SiteDetail> siteDetailsRepository)
{
_siteDetailRepository = siteDetailsRepository;
}
}
您可以使用定义的类型调用存储库方法:
var siteDetails =
await this._siteDetailsRepository
.GetAsync(x =>
x.siteNo == siteNo, //Conditions
null, //Orders
x => x.Country) //Includes
.ConfigureAwait(false);