Exemplo n.º 1
0
        static void Main(string[] args)
        {
            using (var context = new UefaDbContext())
            {
                context.Database.Migrate();
                foreach (var team in context.Teams.Include(t => t.Country))
                {
                    Console.WriteLine($"{team.Name} {team.Country?.Name}");
                }
            }

            using (var context = new UefaDbContext())
            {
                foreach (var team in context.Teams)
                {
                    context.Entry(team)
                    .Reference <Country>(t => t.Country)
                    .Load();

                    context.Entry(team)
                    .Reference <Country>(t => t.Country)
                    .Query()
                    .Where(c => c.Name.Equals("Ukraine"));

                    context.Entry(team)
                    .Collection <Player>(t => t.Players)
                    .Query()
                    .Where(p => p.Name.Equals("Rakitskiy"));

                    Console.WriteLine($"{team.Name} {team.Country?.Name}");
                }
            }

            //using (var context = new UefaDbContext())
            //{
            //    foreach (var team in context.Teams.Select(t => new { t.Name, t.Country }))
            //    {
            //        Console.WriteLine($"{team.Name} {team.Country?.Name}");
            //    }
            //}

            //using (var context = new UefaDbContext())
            //{
            //    context.ChangeTracker.LazyLoadingEnabled = true;
            //    foreach (var team in context.Teams)
            //    {
            //        Console.WriteLine($"{team.Name} {team.Country?.Name}");
            //    }
            //}

            //using (var context = new UefaDbContext())
            //{
            //    foreach (var team in context.Teams)
            //    {
            //        Console.WriteLine($"{team.Name} {team.Players?.Count}");
            //    }
            //}
        }
Exemplo n.º 2
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}");
            }
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 5
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();
            }
        }