Example #1
0
        private static void QueryPetCountByType(string type)
        {
            using var petContext = new PetContext();
            PetCount petCount = petContext.PetCounts
                                .FromSqlRaw("SELECT COUNT(*) as 'Count' FROM Pets WHERE [Type] = {0}", type)
                                .Single();

            Console.WriteLine($"Count: {petCount.Count}");
        }
Example #2
0
        private static void QueryPetsByType(string type)
        {
            using var petContext = new PetContext();
            List <Pet> pets = petContext.Pets
                              .FromSqlRaw("SELECT [PetId], [Name], [Type] FROM Pets WHERE [Type] = {0}", type)
                              .ToList();

            foreach (var pet in pets)
            {
                Console.WriteLine($"Name: {pet.Name}");
            }
        }
Example #3
0
 private static void CreatePets()
 {
     using var petContext = new PetContext();
     petContext.Database.EnsureCreated();
     petContext.Database.ExecuteSqlRaw("DELETE FROM Pets WHERE 1 = 1");
     petContext.Pets.Add(new Pet {
         Name = "Grumpy Cat", Type = "Cat"
     });
     petContext.Pets.Add(new Pet {
         Name = "Smelly Cat", Type = "Cat"
     });
     petContext.Pets.Add(new Pet {
         Name = "Lassie", Type = "Dog"
     });
     petContext.SaveChanges();
 }