public DemoService(
     IOptionsSnapshot <LabSettings> settings,
     LabDbContext dbContext)
 {
     _settings  = settings.Value;
     _dbContext = dbContext;
 }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #7
0
        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;
 }
예제 #9
0
 public ActionResult Index()
 {
     using (var db = new LabDbContext())
     {
         return(View(db.Resources.Include("Category").ToList()));
     }
 }
예제 #10
0
 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));
 }
예제 #12
0
 public void Then預期資料庫的Member資料表有以下資料(Table table)
 {
     using (var dbContext = LabDbContext.Create(ConnectionString))
     {
         var members = dbContext.Members.AsNoTracking().ToList();
         table.CompareToSet(members);
     }
 }
예제 #13
0
 public void Before()
 {
     using (var db = new LabDbContext())
     {
         db.Database.ExecuteSqlCommand("delete from Identity;");
         db.Database.ExecuteSqlCommand("delete from Employee;");
     }
 }
예제 #14
0
 private static void DeleteDb(string connectionString)
 {
     using (var dbContext = new LabDbContext(connectionString))
     {
         if (dbContext.Database.Exists())
         {
             dbContext.Database.Delete();
         }
     }
 }
예제 #15
0
 public static void AssemblyCleanup()
 {
     using (var dbContext = new LabDbContext())
     {
         if (dbContext.Database.Exists())
         {
             dbContext.Database.Delete();
         }
     }
 }
예제 #16
0
        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();
            }
        }
예제 #17
0
 public static void AssemblyCleanup()
 {
     using (var dbContext = LabDbContext.Create())
     {
         var isExist = dbContext.Database.Exists();
         if (isExist)
         {
             //dbContext.Database.Delete();
         }
     }
 }
예제 #18
0
        private void DeleteAll()
        {
            var sql = @"
TRUNCATE TABLE Member
";

            using (var dbContext = new LabDbContext(DbOptionsFactory.DbContextOptions))
            {
                dbContext.Database.ExecuteSqlCommand(sql);
            }
        }
예제 #19
0
        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;
 }
예제 #21
0
        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);
            }
        }
예제 #22
0
        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();
            }
        }
예제 #23
0
        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);
            }
        }
예제 #24
0
        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();
        }
예제 #25
0
 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();
     }
 }
예제 #26
0
        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);
                });
            }
        }
예제 #27
0
        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);
            }
        }
예제 #28
0
        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));
            }
        }
예제 #29
0
        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);
            }
        }
예제 #30
0
        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}");
            }
        }