Пример #1
0
        public static void InitDatabase()
        {
            using (var context = new StarWarsContext())
            {
                if (context.People.Any())
                {
                    context.Database.ExecuteSqlCommand("Delete from dbo.Starships");
                    context.Database.ExecuteSqlCommand("Delete from dbo.People");
                }

                SeedData(context);
            }
        }
Пример #2
0
        public static void SeedData(StarWarsContext context)
        {
            context.People.AddRange(new List <Person> {
                new Person
                {
                    Name      = "Luke Skywalker",
                    HairColor = "Blond",
                    Height    = 1.72,
                    Starships = new List <Starship>
                    {
                        new Starship
                        {
                            Name          = "X-Wing",
                            Cost          = 10000,
                            MaxPassengers = 1,
                        }
                    }
                },
                new Person
                {
                    Name      = "Han Solo",
                    HairColor = "Brown",
                    Height    = 1.8,
                    Starships = new List <Starship>
                    {
                        new Starship
                        {
                            Name          = "Millennium Falcon",
                            Cost          = 100000,
                            MaxPassengers = 6,
                        }
                    }
                }
            });

            context.SaveChanges();
        }
Пример #3
0
        private static void EagerLoad()
        {
            using (var context = new EFCore.StarWarsContext(_optionsBuilder.Options))
            {
                var query = from p in context.People.Include(person => person.Starships)
                            where p.Name.StartsWith("Luke")
                            select p;

                Console.WriteLine("With EF Core:");
                query.ToList().ForEach(p =>
                {
                    Console.WriteLine(p.Name);
                    p.Starships.ForEach(s =>
                    {
                        Console.WriteLine($"   {s.Name}");
                    });
                });
            }
            Console.WriteLine();
            using (var context = new EF6.StarWarsContext())
            {
                var query = from p in System.Data.Entity.QueryableExtensions.Include(
                    context.People, person => person.Starships)
                            where p.Name.StartsWith("Luke")
                            select p;

                Console.WriteLine("With EF 6:");
                query.ToList().ForEach(p =>
                {
                    Console.WriteLine(p.Name);
                    p.Starships.ForEach(s =>
                    {
                        Console.WriteLine($"   {s.Name}");
                    });
                });
            }
        }
Пример #4
0
        private static void MixClientAndSql()
        {
            using (var context = new EFCore.StarWarsContext(_optionsBuilder.Options))
            {
                var query = context.People
                            .FromSql($"Select * from people where {nameof(EFCore.Person.Height)} > {{0}}", 1.5)
                            .OrderByDescending(p => p.Height);

                Console.WriteLine("With EF Core:");
                query.ToList().ForEach(
                    p => Console.WriteLine($"{p.Name}-{p.Height}"));
            }
            Console.WriteLine();
            using (var context = new EF6.StarWarsContext())
            {
                var query = context.People
                            .SqlQuery($"Select * from people where {nameof(EFCore.Person.Height)} > {{0}}", 1.5)
                            .OrderByDescending(p => p.Height);

                Console.WriteLine("With EF 6:");
                query.ToList().ForEach(
                    p => Console.WriteLine($"{p.Name}-{p.Height}"));
            }
        }