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

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

                var unitOfWork = new UnitOfWork(context1);
                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);

                using (var context2 = new BankDepositsContext(optionsBuilder.Options))
                {
                    var unitOfWork2 = new UnitOfWork(context2);
                    unitOfWork2.ExecuteQuery("Delete from dbo.Addresses where address_id = " + address.Id).Wait();
                }

                addressRepository.Delete(address);

                addressRepository.Commit().Wait();

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

            Console.ReadLine();
        }
예제 #2
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();
        }