public DemoService( IOptionsSnapshot <LabSettings> settings, LabDbContext dbContext) { _settings = settings.Value; _dbContext = dbContext; }
public IEnumerable <EmployeeViewModel> GetAllEmployees(out int count) { IEnumerable <EmployeeViewModel> results = null; using (var dbContext = new LabDbContext(this.ConnectionName)) { dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false; dbContext.Configuration.AutoDetectChangesEnabled = false; var selector = dbContext.Employees .Select(p => new EmployeeViewModel { Id = p.Id, Name = p.Name, Age = p.Age, SequenceId = p.SequenceId, }); results = selector.AsNoTracking().ToList(); count = results.Count(); } return(results); }
public IEnumerable <EmployeeViewModel> GetAllEmployeesDetail(out int count) { IEnumerable <EmployeeViewModel> results = null; using (var dbContext = new LabDbContext(this.ConnectionName)) { dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false; dbContext.Configuration.AutoDetectChangesEnabled = false; var selector = dbContext.Identities .Select(p => new EmployeeViewModel { Id = p.Employee.Id, Name = p.Employee.Name, Age = p.Employee.Age, SequenceId = p.Employee.SequenceId, Account = p.Account, Password = p.Password }); count = selector.Count(); if (count == 0) { return(results); } selector = selector.OrderBy(p => p.SequenceId); results = selector.AsNoTracking().ToList(); } return(results); }
public void Query_GUID_Type() { var id = Guid.NewGuid(); var toDb = new Employee { Id = id, Name = "yao", Age = 18 }; using (var db = new LabDbContext()) { db.Employees.Add(toDb); db.SaveChanges(); } using (var db = new LabDbContext()) { var employee = db.Employees .Where(p => p.Id == id) .AsNoTracking() .FirstOrDefault(); Assert.IsNull(employee); } }
public void GetPatient_ParametersMatchExpectedValues() { //Arrange var dbOptions = new DbContextOptionsBuilder <LabDbContext>() .UseInMemoryDatabase(databaseName: $"PatientDb{Guid.NewGuid()}") .Options; var sieveOptions = Options.Create(new SieveOptions()); var fakePatient = new FakePatient { }.Generate(); //Act using (var context = new LabDbContext(dbOptions)) { context.Patients.AddRange(fakePatient); context.SaveChanges(); var service = new PatientRepository(context, new SieveProcessor(sieveOptions)); //Assert var patientById = service.GetPatient(fakePatient.PatientId); patientById.PatientId.Should().Be(fakePatient.PatientId); patientById.FirstName.Should().Be(fakePatient.FirstName); patientById.LastName.Should().Be(fakePatient.LastName); patientById.Sex.Should().Be(fakePatient.Sex); patientById.Gender.Should().Be(fakePatient.Gender); patientById.Race.Should().Be(fakePatient.Race); patientById.Ethnicity.Should().Be(fakePatient.Ethnicity); } }
public IEnumerable <EmployeeViewModel> GetAllEmployeesDetail(out int count) { IEnumerable <EmployeeViewModel> results = null; using (var dbContext = new LabDbContext(this.ConnectionName)) { var selector = dbContext.Identities .Select(p => new EmployeeViewModel { Id = p.Employee.Id, Name = p.Employee.Name, Age = p.Employee.Age, SequenceId = p.Employee.SequenceId, Account = p.Account, Password = p.Password }); count = selector.Count(); if (count == 0) { return(results); } selector = selector.OrderBy(p => p.SequenceId); results = selector.ToList(); } return(results); }
public static LabReportVM GetLabReportVMForReqIds(LabDbContext labDbContext, List <Int64> reqIdList) { List <LabResult_Denormalized_VM> resultsDenormalized = GetResultsDenormalized(labDbContext, reqIdList); LabReportVM retReport = FormatResultsForLabReportVM(resultsDenormalized, labDbContext); return(retReport); }
public UserController(UserManager <AppUser> _userManager, SignInManager <AppUser> _signInManager, RoleManager <AppRole> _roleManager, LabDbContext _context) { userManager = _userManager; signInManager = _signInManager; roleManager = _roleManager; context = _context; }
public ActionResult Index() { using (var db = new LabDbContext()) { return(View(db.Resources.Include("Category").ToList())); } }
public UserController(UserManager <AppUser> _userManager, SignInManager <AppUser> _signInManager, RoleManager <AppRole> _roleManager, LabDbContext _context, IWebHostEnvironment _hostingEnvironment) { userManager = _userManager; signInManager = _signInManager; roleManager = _roleManager; hostingEnvironment = _hostingEnvironment; context = _context; }
public PatientRepository(LabDbContext context, SieveProcessor sieveProcessor) { _context = context ?? throw new ArgumentNullException(nameof(context)); _sieveProcessor = sieveProcessor ?? throw new ArgumentNullException(nameof(sieveProcessor)); }
public void Then預期資料庫的Member資料表有以下資料(Table table) { using (var dbContext = LabDbContext.Create(ConnectionString)) { var members = dbContext.Members.AsNoTracking().ToList(); table.CompareToSet(members); } }
public void Before() { using (var db = new LabDbContext()) { db.Database.ExecuteSqlCommand("delete from Identity;"); db.Database.ExecuteSqlCommand("delete from Employee;"); } }
private static void DeleteDb(string connectionString) { using (var dbContext = new LabDbContext(connectionString)) { if (dbContext.Database.Exists()) { dbContext.Database.Delete(); } } }
public static void AssemblyCleanup() { using (var dbContext = new LabDbContext()) { if (dbContext.Database.Exists()) { dbContext.Database.Delete(); } } }
public static void SeedSamplePatientData(LabDbContext context) { if (!context.Patients.Any()) { context.Patients.Add(new AutoFaker <Patient>()); context.Patients.Add(new AutoFaker <Patient>()); context.Patients.Add(new AutoFaker <Patient>()); context.SaveChanges(); } }
public static void AssemblyCleanup() { using (var dbContext = LabDbContext.Create()) { var isExist = dbContext.Database.Exists(); if (isExist) { //dbContext.Database.Delete(); } } }
private void DeleteAll() { var sql = @" TRUNCATE TABLE Member "; using (var dbContext = new LabDbContext(DbOptionsFactory.DbContextOptions)) { dbContext.Database.ExecuteSqlCommand(sql); } }
private static void InitialDb(string connectionString) { using (var dbContext = new LabDbContext(connectionString)) { if (dbContext.Database.Exists()) { dbContext.Database.Delete(); } dbContext.Database.Initialize(true); } }
public AuthenticationController(LabDbContext dbContext, SignInManager <LabUser> signInManager, UserManager <LabUser> userManager, IPasswordHasher <LabUser> passwordHasher, IConfiguration config) { _dbContext = dbContext; _signInManager = signInManager; _userManager = userManager; _passwordHasher = passwordHasher; _config = config; }
public static void AssemblyInitialize(TestContext context) { using (var dbContext = LabDbContext.Create()) { var isExist = dbContext.Database.Exists(); if (isExist) { //dbContext.Database.Delete(); } //dbContext.Database.Initialize(true); } }
public void Given資料庫的Member資料表已存在以下資料(Table table) { var toDb = table.CreateSet(() => new Member { Remark = TestHook.TestData, CreateAt = TestHook.TestNow, CreateBy = TestHook.TestUserId }); using (var dbContext = LabDbContext.Create(ConnectionString)) { dbContext.Members.AddRange(toDb); dbContext.SaveChanges(); } }
public void Insert() { var toDb = new Employee { Id = Guid.NewGuid(), Name = "yao3", Age = 20 }; using (var db = new LabDbContext()) { db.Employees.Add(toDb); var count = db.SaveChanges(); Assert.AreEqual(1, count); } }
static BenchmarkJoinTest() { string connectionName = "LabDbContext"; //呼叫 BenchmarkManager.Add 傳入需要量測的 Method foreach (var repository in Utility.AdoRepositories) { BenchmarkManager.Add(repository.Key + ".Join", () => { var employees = repository.Value.GetAllEmployeesDetail(out var count); return(new Report { RowCount = count }); }); } foreach (var repository in Utility.Repositories) { BenchmarkManager.Add(repository.Key + ".Join", () => { var employees = repository.Value.GetAllEmployeesDetail(out var count); return(new Report { RowCount = count }); }); } //不檢查migration table Database.SetInitializer <LabDbContext>(null); //載入對應 using (var dbcontext = new LabDbContext(connectionName)) { var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext; var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappingCollection.GenerateViews(new List <EdmSchemaError>()); } //暖機 BenchmarkManager.Warm(); Utility.Warm(); Utility.SwitchLargeDb(); }
public static void AssemblyInitialize(TestContext context) { using (var dbContext = new LabDbContext()) { var toDb = new List <Order> { new Order { Id = Guid.NewGuid(), IsTransform = "Y", Status = "99" }, new Order { Id = Guid.NewGuid(), IsTransform = "N", Status = "10" } }; dbContext.Orders.AddRange(toDb); dbContext.SaveChanges(); } }
public void EF_Select_Mapping() { var expected = new[] { new { IsTransform = "N", TransformDescription = "未轉換", Status = "10", StatusDescription = "已開立" }, new { IsTransform = "Y", TransformDescription = "已轉換", Status = "99", StatusDescription = "已核准" } }; using (var dbContext = new LabDbContext()) { var orders = dbContext.Orders .AsNoTracking() .ToList() .Select(p => new { p.Id, p.IsTransform, TransformDescription = DefineManager.GetLookupByName <EnumTransfer>()[p.IsTransform] .Description, p.Status, StatusDescription = this.GetEnumApprove(p.Status).Description }) .ToList() ; orders.Should() .BeEquivalentTo(expected, option => { option.WithoutStrictOrdering(); return(option); }); } }
public void InsertTwoTable() { var toDb = new Employee { Id = Guid.NewGuid(), Name = "yao", Age = 18, Identity = new Identity { Account = "yao", Password = "******" } }; using (var db = new LabDbContext()) { db.Employees.Add(toDb); var count = db.SaveChanges(); Assert.AreEqual(2, count); } }
public static int DeleteAll(string connectionString) { var sql = @" -- disable referential integrity EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' EXEC sp_MSForEachTable 'DELETE FROM ?' -- enable referential integrity again EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL' "; using (var dbContext = new LabDbContext(connectionString)) { return(dbContext.Database.ExecuteSqlCommand(sql)); } }
private void DeleteAll() { var sql = @" -- disable referential integrity EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' EXEC sp_MSForEachTable 'DELETE FROM ?' -- enable referential integrity again EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL' "; using (var dbContext = LabDbContext.Create()) { dbContext.Database.ExecuteSqlCommand(sql); } }
public void InsertViaEFCore3() { var id = Guid.NewGuid(); var toDb = new Member { Id = id, Name = "yao", Age = 18, }; Console.WriteLine($"寫入資料庫之前 {nameof(toDb.SequenceId)} = {toDb.SequenceId}"); using (var dbContext = new LabDbContext(DbOptionsFactory.DbContextOptions)) { dbContext.Members.Add(toDb); var count = dbContext.SaveChanges(); Assert.AreEqual(true, count != 0); Assert.AreEqual(true, toDb.SequenceId != 0); Console.WriteLine($"寫入資料庫之後 {nameof(toDb.SequenceId)} = {toDb.SequenceId}"); } }