Пример #1
0
        public void SaveNewEquipment(NinjaEquipment equipment, int ninjaId)
        {
            //paying the price of not having a foreign key here.
            //reason #857 why I prefer foreign keys!
            using (var context = new NinjaContext())
            {
                var ninja = context.Ninjas.Find(ninjaId);
                ninja.EquipmentOwned.Add(equipment);

                context.SaveChanges();
            }
        }
Пример #2
0
        public void SaveUpdatedEquipment(NinjaEquipment equipment, int ninjaId)
        {
            //paying the price of not having a foreign key here.
            //reason #858 why I prefer foreign keys!
            using (var context = new NinjaContext())
            {
                var equipmentWithNinjaFromDatabase =
                    context.Equipment.Include(n => n.Ninja).FirstOrDefault(e => e.Id == equipment.Id);

                context.Entry(equipmentWithNinjaFromDatabase).CurrentValues.SetValues(equipment);

                context.SaveChanges();
            }
        }
Пример #3
0
        public List <Ninja> GetQueryableNinjasWithClan(string query, int page, int pageSize)
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = message => Debug.WriteLine(message);
                var linqQuery = context.Ninjas.Include(n => n.Clan);
                if (!string.IsNullOrEmpty(query))
                {
                    linqQuery = linqQuery.Where(n => n.Name.Contains(query));
                }
                if (page > 0 && pageSize > 0)
                {
                    linqQuery = linqQuery.OrderBy(n => n.Name).Skip(page - 1).Take(pageSize);
                }

                return(linqQuery.ToList());
            }
        }
Пример #4
0
        public static void NewDbWithSeed()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <NinjaContext>());

            using (var context = new NinjaContext())
            {
                if (context.Ninjas.Any())
                {
                    return;
                }

                var vtClan = context.Clans.Add(new Clan {
                    ClanName = "Vermont Clan"
                });
                var turtleClan = context.Clans.Add(new Clan {
                    ClanName = "Turtles"
                });
                var amClan = context.Clans.Add(new Clan {
                    ClanName = "American Ninja Warriors"
                });
            }
        }