代码之家  ›  专栏  ›  技术社区  ›  Bryan Stump

实体框架核心重用一组include语句

  •  1
  • Bryan Stump  · 技术社区  · 7 年前

    如何清理我的实体“包含”以重用相同的语句集?我试着在保持干燥的同时再利用一套内含物。

    以前

    _context.Accounts
                  .Include(x=> x.Status)
                  .Include(x=> x.Type)
                  .Include(x=> x.Phones)
                  .Include(x=> x.Users)
                  .Include(x=> x.Admins)
    

    后:

    _context.Accounts.CustomIncludes()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   J. Allen    7 年前

    试试这个:

    public static class DataExtensions
    {
        public static Microsoft.EntityFrameworkCore.Query.IIncludableQueryable<Account, List<Admin>> CustomIncludes(this DbSet<Account> accounts)
        {
            return accounts
                            .Include(p => p.Status)
                            .Include(p => p.Type)
                            .Include(p => p.Phones)
                            .Include(p => p.Users)
                            .Include(p => p.Admins);
        }
    }
    

    那你可以说

    context.Accounts.CustomIncludes();
    
    推荐文章