C
C#2y ago
Gage

✅ EF Core, All models into one collection

I would like to make a single collection of all models in the database. What is a good approach for this?
3 Replies
jrusbatch
jrusbatch2y ago
var allObjects = new List<object>();

allObjects.AddRange(db.Foos);
allObjects.AddRange(db.Bars);
var allObjects = new List<object>();

allObjects.AddRange(db.Foos);
allObjects.AddRange(db.Bars);
Gage
Gage2y ago
Thank you does this look good?
[Authorize]
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
public IList<object> AllModels = new List<object>();

public IndexModel(ApplicationDbContext context)
{
_context = context;
}

public void OnGet()
{
AllModels.Add(_context.Activities.Include(a => a.Section).ToListAsync());
AllModels.Add( _context.Buildings.ToListAsync());
AllModels.Add(_context.Courses.Include(c => c.Department).ToListAsync());
AllModels.Add( _context.Departments.ToListAsync());
AllModels.Add(_context.Grades.Include(g => g.Activity).Include(g => g.Student).ToListAsync());
AllModels.Add(_context.Rooms.Include(r => r.Building).ToListAsync());
AllModels.Add(_context.Sections.Include(s => s.Course).Include(s => s.Room).Include(s => s.Teacher).ToListAsync());
AllModels.Add(_context.Students.Include(s => s.Major).ToListAsync());
AllModels.Add(_context.Teachers.Include(t => t.Department).Include(t => t.Office).ToListAsync());
}
[Authorize]
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
public IList<object> AllModels = new List<object>();

public IndexModel(ApplicationDbContext context)
{
_context = context;
}

public void OnGet()
{
AllModels.Add(_context.Activities.Include(a => a.Section).ToListAsync());
AllModels.Add( _context.Buildings.ToListAsync());
AllModels.Add(_context.Courses.Include(c => c.Department).ToListAsync());
AllModels.Add( _context.Departments.ToListAsync());
AllModels.Add(_context.Grades.Include(g => g.Activity).Include(g => g.Student).ToListAsync());
AllModels.Add(_context.Rooms.Include(r => r.Building).ToListAsync());
AllModels.Add(_context.Sections.Include(s => s.Course).Include(s => s.Room).Include(s => s.Teacher).ToListAsync());
AllModels.Add(_context.Students.Include(s => s.Major).ToListAsync());
AllModels.Add(_context.Teachers.Include(t => t.Department).Include(t => t.Office).ToListAsync());
}
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server
More Posts