Beispiel #1
0
        static void UpdateUseCase()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["BankDepositsDatabase"].ConnectionString;
            var optionsBuilder   = new DbContextOptionsBuilder <BankDepositsContext>()
                                   .UseSqlServer(connectionString);

            using (var context = new BankDepositsContext(optionsBuilder.Options))
            {
                var rand = new Random();

                var unitOfWork = new UnitOfWork(context);
                IAddressRepository addressRepository = new AddressRepository(unitOfWork);

                var address = new Address();
                addressRepository.Create(address);
                address.Apartment = 5;
                address.House     = 10;
                address.Housing   = "A";
                address.Street    = new Street()
                {
                    Locality = new Locality()
                    {
                        LocalityName = "Partizanski" + rand.NextDouble(),
                        LocalityType = LocalityType.City,
                        Region       = "Minsk"
                    },
                    StreetName = "Frolikova",
                    Postcode   = 220037
                };

                addressRepository.Commit().Wait();

                var addresses = addressRepository.GetBy(null, a => a.Street).ToListAsync().Result;
                PrintAddresses(addresses);

                var adr = addressRepository.GetById(address.Id, a => a.Street).Result;
                context.Entry(adr).State = EntityState.Detached;
                adr.Housing = "OLD";
                adr.House   = 0;

                addressRepository.Update(adr);

                addressRepository.Commit().Wait();

                addresses = addressRepository.GetBy(null, a => a.Street).ToListAsync().Result;
                PrintAddresses(addresses);
            }

            Console.ReadLine();
        }