public override void Task2()
        {
            var town = new Towns()
            {
                Name = "Moscow"
            };

            Context.Towns.Add(town);
            Context.SaveChanges(); //call to get town ID

            var address = new Addresses()
            {
                AddressText = "27/1 Lubyanka", Town = town
            };

            Context.Addresses.Add(address);
            Context.SaveChanges(); //call to get address ID

            var browns = (
                from employee in Context.Employees
                where employee.LastName == "Brown"
                select employee
                ).ToList();

            browns.ForEach(t => t.Address = address);

            Context.SaveChanges(); //call to save addresses
        }
Beispiel #2
0
        //Task 2
        static string AddNewAdressForBrown()
        {
            var town = new Towns()
            {
                Name = "Moscow"
            };

            _context.Towns.Add(town);
            _context.SaveChanges();

            var address = new Addresses()
            {
                AddressText = "Brown street", Town = town
            };

            _context.Addresses.Add(address);
            _context.SaveChanges();

            var browns = (
                from employee in _context.Employees
                where employee.LastName == "Brown"
                select employee
                ).ToList();

            browns.ForEach(e => e.Address = address);
            var sb = new StringBuilder();

            foreach (var b in browns)
            {
                sb.AppendLine($"{b.FirstName} {b.LastName} {b.MiddleName} {b.JobTitle} {b.DepartmentId} {b.ManagerId} {b.Address.AddressText}");
            }
            return(sb.ToString().TrimEnd());
        }
Beispiel #3
0
        static void Task_2()
        {
            Console.Write("Введите город: ");
            string towns = Console.ReadLine();

            Console.Write("Введите адрес: ");
            var newAddress = Console.ReadLine();

            var selectTown = context.Towns.Where(t => t.Name.Contains(towns)).FirstOrDefault();

            if (selectTown != null)
            {
                Addresses addAddress = new Addresses()
                {
                    AddressText = newAddress,
                    TownId      = selectTown.TownId
                };

                context.Addresses.Add(addAddress);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException.Message);
                }

                UpdateUsers(newAddress);
            }
            else
            {
                Console.WriteLine("Данного города не существует в базе, желаете добавить?");
                Console.Write("Введите Y/N (Да/Нет): ");
                string choose = Console.ReadLine();

                switch (choose)
                {
                case "Y":
                    Towns addTowns = new Towns()
                    {
                        Name = towns
                    };

                    context.Towns.Add(addTowns);

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.InnerException.Message);
                        break;
                    }

                    var twId = context.Towns.Where(t => t.Name.Contains(towns)).FirstOrDefault();

                    if (twId == null)
                    {
                        Console.WriteLine("Не удалось добавить город, повторите попытку позже..");
                        break;
                    }
                    else
                    {
                        Addresses addAddressWithTown = new Addresses()
                        {
                            AddressText = newAddress,
                            TownId      = twId.TownId
                        };

                        context.Addresses.Add(addAddressWithTown);

                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.InnerException.Message);
                            break;
                        }

                        UpdateUsers(newAddress);
                    }

                    break;

                case "N":
                    Console.WriteLine("\nЗавершение работы..");
                    break;
                }
            }
        }
Beispiel #4
0
        static void Task_2_SQL()
        {
            Console.Write("Введите город: ");
            string towns = Console.ReadLine();

            Console.Write("Введите адрес: ");
            var newAddress = Console.ReadLine();

            string nativeSqlQuery = $"SELECT * FROM Towns AS t " +
                                    $"WHERE t.Name = @town";
            SqlParameter param = new SqlParameter("@town", towns);

            var selectTown = context.Towns.FromSqlRaw(nativeSqlQuery, param).FirstOrDefault();

            if (selectTown != null)
            {
                Addresses addAddress = new Addresses()
                {
                    AddressText = newAddress,
                    TownId      = selectTown.TownId
                };

                context.Addresses.Add(addAddress);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException.Message);
                }

                UpdateUsers(newAddress);
            }
            else
            {
                Console.WriteLine("Данного города не существует в базе, желаете добавить?");
                Console.Write("Введите Y/N (Да/Нет): ");
                string choose = Console.ReadLine();

                switch (choose)
                {
                case "Y":
                    Towns addTowns = new Towns()
                    {
                        Name = towns
                    };

                    context.Towns.Add(addTowns);

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.InnerException.Message);
                        break;
                    }

                    string nativeTwId = $"SELECT * FROM Towns AS t " +
                                        $"WHERE t.Name = @town";
                    SqlParameter paramTwId = new SqlParameter("@town", towns);

                    var twId = context.Towns.FromSqlRaw(nativeTwId, paramTwId).FirstOrDefault();

                    if (twId == null)
                    {
                        Console.WriteLine("Не удалось добавить город, повторите попытку позже..");
                        break;
                    }
                    else
                    {
                        Addresses addAddressWithTown = new Addresses()
                        {
                            AddressText = newAddress,
                            TownId      = twId.TownId
                        };

                        context.Addresses.Add(addAddressWithTown);

                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.InnerException.Message);
                            break;
                        }

                        UpdateUsers_SQL(newAddress);
                    }

                    break;

                case "N":
                    Console.WriteLine("\nЗавершение работы..");
                    break;
                }
            }
        }