private void CreateButton_Click(object sender, RoutedEventArgs e) { if (IsDataCorrect()) { using (var db = new ClientsEntities()) { var mainAccount = new Account(); var percentAccount = new Account(); mainAccount.ClientID = _accountModel.ClientId; percentAccount.ClientID = _accountModel.ClientId; mainAccount.DepositTypeID = db.GetDepositTypeByName(_accountModel.DepositType).DepositTypeID; percentAccount.DepositTypeID = mainAccount.DepositTypeID; mainAccount.AccountNumber = _accountModel.AccountNumber; var part1 = _accountModel.AccountNumber.Substring(0, 9); var part2 = GetCorrectAccountCountPart(Convert.ToInt32(_accountModel.AccountNumber.Substring(9, 3)) + 1); var part3 = new Random().Next(0, 9); percentAccount.AccountNumber = part1 + part2 + part3; mainAccount.MoneyAmount = Convert.ToDouble(_accountModel.MoneyAmount); if (mainAccount.DepositTypeID == 3 || mainAccount.DepositTypeID == 4) { percentAccount.MoneyAmount = Math.Round(mainAccount.MoneyAmount * Convert.ToDouble(_accountModel.Percents) / 100, 2); } else { percentAccount.MoneyAmount = 0; } mainAccount.StartDate = _accountModel.StartDate; percentAccount.StartDate = _accountModel.StartDate; mainAccount.EndDate = _accountModel.EndDate; percentAccount.EndDate = _accountModel.EndDate; mainAccount.DaysCount = 0; percentAccount.DaysCount = 0; mainAccount.CurrencyID = db.GetCurrencyByName(_accountModel.Currency).CurrencyID; percentAccount.CurrencyID = mainAccount.CurrencyID; mainAccount.IsClosed = false; percentAccount.IsClosed = false; db.Account.Add(percentAccount); db.SaveChanges(); mainAccount.PercentAccountID = db.GetAccountByAccountNumber(percentAccount.AccountNumber).AccountID; db.Account.Add(mainAccount); var bankResources = db.BankResourse.ToList(); if (mainAccount.DepositTypeID == 3 || mainAccount.DepositTypeID == 4) { bankResources[0].PhysicalMoney -= ConvertCurrencyToByn(mainAccount.CurrencyID) * mainAccount.MoneyAmount; } else { bankResources[0].PhysicalMoney += ConvertCurrencyToByn(mainAccount.CurrencyID) * mainAccount.MoneyAmount; } db.SaveChanges(); if (mainAccount.DepositTypeID == 3 || mainAccount.DepositTypeID == 4) { var additionalAcc = new Account(); part1 = _accountModel.AccountNumber.Substring(0, 9); part2 = GetCorrectAccountCountPart(Convert.ToInt32(_accountModel.AccountNumber.Substring(9, 3)) + 1); part3 = new Random().Next(0, 9); additionalAcc.AccountNumber = part1 + part2 + part3; additionalAcc.ClientID = mainAccount.ClientID; additionalAcc.CurrencyID = mainAccount.CurrencyID; additionalAcc.DaysCount = 0; additionalAcc.DepositTypeID = 1; additionalAcc.IsClosed = true; additionalAcc.StartDate = mainAccount.StartDate; additionalAcc.EndDate = mainAccount.EndDate; additionalAcc.MoneyAmount = mainAccount.MoneyAmount; db.Account.Add(additionalAcc); db.SaveChanges(); var card = new Card(); card.AccountID = db.GetAccountByAccountNumber(additionalAcc.AccountNumber).AccountID; card.Number = mainAccount.AccountNumber + new Random().Next(100, 999); card.PIN = new Random().Next(1000, 9999); db.Card.Add(card); } db.SaveChanges(); this.Close(); } } }