Exemplo n.º 1
0
        public static void Main()
        {
            var exit = false;

            while (!exit)
            {
                Console.WriteLine("What do you want to do?\n[new|list|update|delete|exit] [dog|person|ownership]");
                var    command = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                Type   entityType;
                object entity;
                using var dbContext = new DogFarmDbContext();
                switch (command[0])
                {
                case "new":
                    entityType = GetEntityType();
                    entity     = Activator.CreateInstance(entityType);
                    SetPropertiesOrSave(entity);
                    dbContext.Add(entity);
                    dbContext.SaveChanges();
                    break;

                case "list":
                    entityType = GetEntityType();
                    var entities = (IEnumerable)typeof(DogFarmDbContext).GetMethod(nameof(DogFarmDbContext.Set)).MakeGenericMethod(entityType).Invoke(dbContext, null);
                    if (entities.Cast <object>().Count() == 0)
                    {
                        Console.WriteLine($"No entities stored of type {entityType}.");
                    }
                    else
                    {
                        foreach (var item in entities)
                        {
                            Console.WriteLine(item);
                        }
                    }
                    break;

                case "update":
                    entityType = GetEntityType();
                    entity     = dbContext.Find(entityType, ReadId());
                    SetPropertiesOrSave(entity);
                    dbContext.SaveChanges();
                    break;

                case "delete":
                    entityType = GetEntityType();
                    entity     = dbContext.Find(entityType, ReadId());
                    dbContext.Remove(entity);
                    dbContext.SaveChanges();
                    break;

                case "exit":
                    exit = true;
                    break;

                default:
                    Console.WriteLine($"Unknown command: '{command}'.");
                    break;
                }
Exemplo n.º 2
0
        private static void UpdateDogBirthDate(int dogId, DateTime?birthDate)
        {
            using var dbContext = new DogFarmDbContext();
            var dog = dbContext.Dogs.Find(dogId);

            dog.BirthDate = birthDate;
            dbContext.SaveChanges();
        }
Exemplo n.º 3
0
        private static void DeleteDog(int dogId)
        {
            using var dbContext = new DogFarmDbContext();
            var dog = dbContext.Dogs.Find(dogId);

            dbContext.Dogs.Remove(dog);
            dbContext.SaveChanges();
        }
Exemplo n.º 4
0
 private static void AddDogToDatabase(Dog newDog)
 {
     using var dbContext = new DogFarmDbContext();
     dbContext.Dogs.Add(newDog);
     dbContext.SaveChanges();
 }