Пример #1
0
        private static void Insert(UefaDbContext context)
        {
            var countries = new List <Country>();

            for (int i = 0; i < 2; i++)
            {
                countries.Add(new Country
                {
                    Name = $"France {i}"
                });
            }

            context.Countries.AddRange(countries);
            context.SaveChanges();

            countries = new List <Country>();
            for (int i = 0; i < 10; i++)
            {
                countries.Add(new Country
                {
                    Name = $"France {i}"
                });
            }

            context.Countries.AddRange(countries);
            context.SaveChanges();
        }
Пример #2
0
        public async Task <IActionResult> AddClubs(ClubsViewModel cvm)
        {
            var Claimuser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (ModelState.IsValid)
            {
                var ClubInfo = new Clubs
                {
                    ClubName    = cvm.ClubName,
                    ClubManager = cvm.ClubManager,
                    UserId      = Claimuser.Id.ToString()
                };
                _dbContext.Add(ClubInfo);
                var CName = _dbContext.tblClubs.FirstOrDefault(f => f.ClubName == cvm.ClubName);
                if (CName == null)
                {
                    _dbContext.SaveChanges();
                    ViewBag.Message = "Club is added successfully!";
                }
                else
                {
                    ViewBag.Error = "Club Name already taken!";
                }
            }
            return(View(cvm));
        }
Пример #3
0
        private static void Update(UefaDbContext context)
        {
            foreach (var country in context.Countries.TagWith("Update Countries"))
            {
                country.Name = country.Name + " (Updated)";
            }

            context.SaveChanges();
        }
Пример #4
0
        private static void ShowPrivateProperty(UefaDbContext context)
        {
            var firstPlayer = context.Players.Find(1);

            firstPlayer.Phone = "093-344-44-19";
            context.SaveChanges();
            foreach (var player in context.Players)
            {
                Console.WriteLine($"{player.Name} {player.Phone}");
            }
        }
Пример #5
0
 static void Main(string[] args)
 {
     using (var context = new UefaDbContext())
     {
         context.Database.EnsureDeleted();
         context.Database.Migrate();
         context.Players.Add(new Player()
         {
             Name = "Vitek"
         });
         context.SaveChanges();
         ShowPrivateProperty(context);
         ShowShadowProperty(context);
     }
 }
Пример #6
0
        private static void ShowShadowProperty(UefaDbContext context)
        {
            Player firstPlayer = context.Players.Find(1);
            var    lastName    = context.Entry(firstPlayer).Property <string>("LastName").CurrentValue;

            Console.WriteLine($"Get property value {lastName}");

            context.Entry(firstPlayer).Property <string>("LastName").CurrentValue = "Ivanov";
            context.SaveChanges();

            foreach (var player in context.Players.Where(p => context.Entry(p).Property <string>("LastName").CurrentValue == "Ivanov"))
            {
                Console.WriteLine(
                    $"{player.Name} {player.Phone} {context.Entry(player).Property<string>("LastName").CurrentValue}");
            }
        }
Пример #7
0
        private static void Update_2()
        {
            using (var context = new UefaDbContext())
            {
                Console.WriteLine("Update_2 EntityState.Modified");
                var teamToUpdate = new Team()
                {
                    Id      = 1,
                    Country = context.Countries.Find(1),
                    Name    = "Shahtar (Updated_2)"
                };

                context.Entry(teamToUpdate).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
Пример #8
0
        private static void Update_3()
        {
            using (var context = new UefaDbContext())
            {
                Console.WriteLine("Update_3 Explicit property modified");
                var teamToUpdate = new Team()
                {
                    Id      = 1,
                    Country = context.Countries.Find(1),
                    Name    = "Shahtar (Updated_3)"
                };

                context.Attach(teamToUpdate);
                context.Entry(teamToUpdate).Property(t => t.Name).IsModified = true;
                context.SaveChanges();
            }
        }
Пример #9
0
        private static void Update_1()
        {
            using (var context = new UefaDbContext())
            {
                var team = context.Teams.Find(1);

                var teamToUpdate = new Team()
                {
                    Id      = 1,
                    Country = context.Countries.Find(1),
                    Name    = "Shahtar (Updated)"
                };

                // update all properties include country
                context.Teams.Update(teamToUpdate);
                context.SaveChanges();
            }
        }
Пример #10
0
        private static void Update_1_1()
        {
            using (var context = new UefaDbContext())
            {
                Console.WriteLine("Update");
                var team = context.Teams.Find(1);

                var teamToUpdate = new Team()
                {
                    Id      = 1,
                    Country = context.Countries.Find(1),
                    Name    = "Shahtar (Updated)"
                };

                context.Entry(team).State = EntityState.Detached;
                context.Teams.Update(teamToUpdate);
                context.SaveChanges();
            }
        }
Пример #11
0
        private static void SeedDataUpdateExamples()
        {
            using (var context = new UefaDbContext())
            {
                context.Countries.AddRange(new Country()
                {
                    Name = "Ukraine"
                }, new Country()
                {
                    Name = "Spain"
                });

                context.Teams.Add(new Team()
                {
                    Name = "Shahtar"
                });

                context.SaveChanges();
                Console.Clear();
            }
        }
Пример #12
0
        private static void Update_5()
        {
            using (var context = new UefaDbContext())
            {
                Console.WriteLine("Update_5 Update properties");
                var teamToUpdate = new Team()
                {
                    Id = 1
                };

                context.Attach(teamToUpdate);
                teamToUpdate.Name = "Shahtar (Updated_5)";

                var country2 = new Country()
                {
                    Id = 2
                };
                context.Attach(country2);

                teamToUpdate.Country = country2;

                context.SaveChanges();
            }
        }
Пример #13
0
        private static void Update_4()
        {
            using (var context = new UefaDbContext())
            {
                Console.WriteLine("Update_4 Update properties");
                var teamToUpdate = new Team()
                {
                    Id = 1
                };

                context.Attach(teamToUpdate);
                teamToUpdate.Name = "Shahtar (Updated_4)";

                // Cannot insert explicit value for identity column in table 'Countries' when
                // IDENTITY_INSERT is set to OFF.
                var country2 = new Country()
                {
                    Id = 2
                };
                teamToUpdate.Country = country2;

                context.SaveChanges();
            }
        }