public void TestLoadByRoleDb() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using (var context = new Chapter06Context(options)) { context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); } using (var context = new Chapter06Context(options)) { //ATTEMPT var devDept = context.Employees //#A .Include(x => x.WorksForMe) //#B .Where(x => x.WhatTheyDo.HasFlag(Roles.Development)) //#C .ToList(); /******************************************************** #A The database holds all the Employees #B One Include is all that you need - relational fixup will work out what is linked to what #C This filters the employees down to ones that work in Development **********************************************/ //VERIFY devDept.Count.ShouldEqual(7); } }
public void TestThenIncludeWorksForMe() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using (var context = new Chapter06Context(options)) { context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); } using (var context = new Chapter06Context(options)) { //ATTEMPT var all = context.Employees .Include(x => x.WorksForMe) .ThenInclude(x => x.WorksForMe) .ToList(); //VERIFY all.Count.ShouldEqual(11); all.Count(x => x.Manager != null).ShouldEqual(10); all.Count(x => x.WorksForMe.Any()).ShouldEqual(5); var top = all.Single(x => x.Manager == null); top.ShowHierarchical(s => _output.WriteLine(s)); } }
public void TestLoadByRoleDbShowQuery() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using (var context = new Chapter06Context(options)) { context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); } using (var context = new Chapter06Context(options)) { //ATTEMPT var query = context.Employees .Include(x => x.WorksForMe) .Where(x => x.WhatTheyDo.HasFlag(Roles.Development)); var devDept = query.ToList(); //VERIFY _output.WriteLine(query.ToQueryString()); devDept.Count.ShouldEqual(7); var cto = devDept.Single(x => x.Manager == null); cto.ShowHierarchical(s => _output.WriteLine(s)); } }
public void TestLoadWorksForMeAsNoTrackingWithIdentityResolution() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using var context = new Chapter06Context(options); context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); context.ChangeTracker.Clear(); //ATTEMPT var all = context.Employees .AsNoTrackingWithIdentityResolution() .Include(x => x.WorksForMe) .ToList(); //VERIFY all.Count.ShouldEqual(11); all.Count(x => x.Manager != null).ShouldEqual(10); all.Count(x => x.WorksForMe.Any()).ShouldEqual(5); var top = all.Single(x => x.Manager == null); top.ShowHierarchical(s => _output.WriteLine(s)); }
public void TestAddTestEmployeesToDb() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using var context = new Chapter06Context(options); context.Database.EnsureCreated(); //ATTEMPT context.AddTestEmployeesToDb(); //VERIFY context.Employees.Count().ShouldEqual(11); }
public void TestLoadManager() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using var context = new Chapter06Context(options); context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); context.ChangeTracker.Clear(); //ATTEMPT var all = context.Employees.Include(x => x.Manager) .ToList(); //VERIFY all.Count.ShouldEqual(11); all.Count(x => x.Manager != null).ShouldEqual(10); all.Count(x => x.WorksForMe != null).ShouldEqual(5); all.Count(x => x.WorksForMe == null).ShouldEqual(6); }
public void TestLoadWorksForMeNoWorkingAsNoTracking() { //SETUP var options = SqliteInMemory.CreateOptions <Chapter06Context>(); using (var context = new Chapter06Context(options)) { context.Database.EnsureCreated(); context.AddTestEmployeesToDb(); } using (var context = new Chapter06Context(options)) { //ATTEMPT var all = context.Employees .AsNoTracking() .Include(x => x.WorksForMe) .ToList(); //VERIFY all.Count.ShouldEqual(11); all.Count(x => x.Manager == null).ShouldEqual(11); all.Count(x => x.WorksForMe.Any()).ShouldEqual(5); } }