Exemple #1
0
 /// <summary>
 /// FINDS ALL THE NAMES THAT CONTAIN SHAN
 /// </summary>
 private static void FindAName()
 {
     using (var context = new NinjaContext())
     {
         var ninjas = context.Ninjas.Where(x=>x.Name.Contains("Shan"));
     }
 }
 public NinjaEquipment GetEquipmentById(int id)
 {
     using (var context = new NinjaContext())
     {
         return context.Equipment.Find(id);
     }
 }
 public IEnumerable GetClanList()
 {
     using (var context = new NinjaContext()) {
     return context.Clans.AsNoTracking().OrderBy(c => c.ClanName)
       .Select(c => new { c.Id, c.ClanName }).ToList();
       }
 }
 public ConnectedRepository()
 {
     _context = new NinjaContext();
     _ninjas = _context.Ninjas;
     _clans = _context.Clans;
     _equipment = _context.Equipment;
 }
Exemple #5
0
        private static void InsertMultipleNinjas()
        {
            var ninja2 = new Ninja
            {
                Name = "SampsonSan",
                ServedInOniwaban = false,
                DateOfBirth = new DateTime(2008, 1, 28),
                ClanId = 1
            };

            var ninjas22 = new Ninja
            {
                Name = "SampsonSan",
                ServedInOniwaban = false,
                DateOfBirth = new DateTime(2008, 1, 28),
                ClanId = 1
            };

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Ninjas.AddRange(new List<Ninja> { ninja2, ninjas22});
                context.SaveChanges();
            }
        }
Exemple #6
0
 private static void SimpleNinjaGraphQuery()
 {
     Ninja ninja;
     var LoadingType = "lazy";
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         switch (LoadingType)
         {
             case "eager":
                  ninja = context.Ninjas
                     .Include(n => n.EquipmentOwned)
                     .FirstOrDefault(n => n.Name == "Joe Ortiz");
                 break;
             case "explicit":
                  ninja = context.Ninjas.FirstOrDefault(n => n.Name.StartsWith("Joe Ortiz"));
                 Console.WriteLine("Ninja Retrieved:" + ninja.Name);
                 context.Entry(ninja).Collection(n => n.EquipmentOwned).Load();
                 break;
             case "lazy":
                 ninja = context.Ninjas.FirstOrDefault(n => n.Name.StartsWith("Joe Ortiz"));
                 Console.WriteLine("Ninja Retrieved:" + ninja.Name);
                 Console.WriteLine("Ninja Equipment Count: " + ninja.EquipmentOwned.Count()); 
                 //mark EquipmentOwned property as virtual. Be careful with performance issues.
                 break;
         }
     }
 }
Exemple #7
0
        private static void InsertNinjaWithEquipment()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = new Ninja()
                {
                    Name = "Joe Ortiz",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1990, 1, 1),
                    ClanId = 1
                };

                var muscles = new NinjaEquipment
                {
                    Name = "Muscles",
                    Type = EquipmentType.Tool
                };

                var spunk = new NinjaEquipment()
                {
                    Name = "Spunk",
                    Type = EquipmentType.Weapon
                };

                context.Ninjas.Add(ninja);
                ninja.EquipmentOwned.Add(muscles);
                ninja.EquipmentOwned.Add(spunk);
                context.SaveChanges();

            }
        }
 public List<Ninja> GetNinjasWithClan()
 {
     using (var context = new NinjaContext()) {
     //return context.Ninjas.Include(n => n.Clan).ToList();
     return context.Ninjas.AsNoTracking().Include(n => n.Clan).ToList();
       }
 }
Exemple #9
0
        private static void DeleteNinja()
        {
            Ninja ninja;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                // SELECT TOP 1 FROM Ninjas
                ninja = context.Ninjas.FirstOrDefault();
            }

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                //context.Ninjas.Attach(ninja);
                //context.Ninjas.Remove(ninja);   // new context, without Attach ninja not found
                context.Entry(ninja).State = EntityState.Deleted;

                // DELETE Ninjas WHERE Id = 8
                context.SaveChanges();
            }

            //using (var context = new NinjaContext())
            //{
            //    context.Database.Log = Console.WriteLine;

            //    var ninja = context.Ninjas.FirstOrDefault();
            //    context.Ninjas.Remove(ninja);
            //    context.SaveChanges();
            //}
        }
 public Ninja GetNinjaById(int id)
 {
     using (var context = new NinjaContext()) {
     return context.Ninjas.Find(id);
     // return context.Ninjas.AsNoTracking().SingleOrDefault(n => n.Id == id);
       }
 }
 public Ninja GetNinjaWithEquipment(int id)
 {
     using (var context = new NinjaContext()) {
     return context.Ninjas.AsNoTracking().Include(n => n.EquipmentOwned)
       .FirstOrDefault(n => n.Id == id);
       }
 }
 public void DeleteNinja(int ninjaId)
 {
     using (var context = new NinjaContext()) {
     var ninja = context.Ninjas.Find(ninjaId);
     context.Entry(ninja).State = EntityState.Deleted;
     context.SaveChanges();
       }
 }
Exemple #13
0
 private static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var query = context.Ninjas;
         var ninjas = query.ToList();
     }
 }
 public List<Ninja> GetNinjasWithClan()
 {
     using (var context = new NinjaContext())
     {
         return context.Ninjas
             .AsNoTracking() // PERFORMANCE: do not provide tracking information as we will destroy context
             .Include(n => n.Clan)
             .ToList();
     }
 }
Exemple #15
0
 private static void DeleteNinja()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninja = context.Ninjas.FirstOrDefault();
         context.Ninjas.Remove(ninja);
         context.SaveChanges();
     }
 }
Exemple #16
0
        // use a stored procedure to delete an entry in a single db call
        private static void DeleteNinjaViaStoredProcedure()
        {
            var keyVal = 3;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Database.ExecuteSqlCommand("exec DeleteNinjaViaId {0}", keyVal);
            }
        }
Exemple #17
0
 private static void ProjectQuery()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninjas = context.Ninjas
             .Select(n => new {n.Name, n.DateOfBirth, n.EquipmentOwned})
             .ToList();
     }
 }
 private static void DeleteNinjaWithKeyValue()
 {
     var keyval = 1;
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninja = context.Ninjas.Find(keyval);
         context.Ninjas.Remove(ninja);
         context.SaveChanges();
     }
 }
Exemple #19
0
        /// <summary>
        /// THIS WILL RETURN NULL or FIRST IF YOU USE FIRST
        /// </summary>
        private static void FindByDateOfBirth()
        {
            using (var context = new NinjaContext())
            {
                //FirstOrDefault will retrun a null if there are no results
                var ninjas = context.Ninjas.Where(x => x.DateOfBirth == new DateTime(1980,1,1)).FirstOrDefault();

                //This will throw exception if there are no results
                // var ninjas = context.Ninjas.Where(x => x.DateOfBirth == new DateTime(1980, 1, 1)).First();
            }
        }
Exemple #20
0
 private static void DeleteNinja()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var n = context.Ninjas.FirstOrDefault();
         context.Entry(n).State = EntityState.Modified;
         context.Ninjas.Remove(n);
         context.SaveChanges();
     }
 }
Exemple #21
0
 private static void QueryAndUpdateNinja()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var query = context.Ninjas;
         var ninja = query.FirstOrDefault();
         ninja.ServedInOniwaban = !ninja.ServedInOniwaban;
         context.SaveChanges();
     }
 }
 public Ninja GetNinjaWithEquipmentAndClan(int id)
 {
     using (var context = new NinjaContext())
     {
         return(context
                .Ninjas
                .AsNoTracking()
                .Include(n => n.EquipmentOwned)
                .Include(n => n.Clan)
                .FirstOrDefault(n => n.Id == id));
     }
 }
 public IEnumerable GetClans()
 {
     using (var context = new NinjaContext())
     {
         return(context
                .Clans
                .AsNoTracking()
                .OrderBy(c => c.ClanName)
                .Select(c => new { c.Id, c.ClanName })
                .ToList());
     }
 }
        public void SaveUpdatedEquipment(NinjaEquipment equipment, int postId)
        {
            //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.Post).FirstOrDefault(e => e.Id == equipment.Id);

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

                context.SaveChanges();
            }
        }
        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();
            }
        }
Exemple #26
0
        public static void NewDBWithSeed()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <NinjaContext>());
            using (var context = new NinjaContext())
            {
                if (context.Ninjas.Any())
                {
                    return;
                }

                var vtclas = context.Clans.Add(new Clan {
                    ClanName = "White Clan"
                });
            }
        }
        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();
              }
        }
        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());
            }
        }
Exemple #29
0
        private static void InsertNinja()
        {
            var ninja = new Ninja
            {
                Name = "SampsonSan",
                ServedInOniwaban = false,
                DateOfBirth = new DateTime(2008, 1, 28),
                ClanId = 1
            };

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Ninjas.Add(ninja);
                context.SaveChanges();
            }
        }
Exemple #30
0
        private static void InsertNinjaWithEquipment()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = new Ninja
                {
                  Name = "Kacy Catanzaro" , ServedInOniwaban = true, DateOfBirth = new DateTime (1975,7,2), ClanId = 1
                };
                var muscles = new NinjaEquipment { Name = "Muscles", Type = EquipmentType.Blade };
                var spunk = new NinjaEquipment { Name = "Spunk", Type = EquipmentType.Sword };

                context.Ninjas.Add(ninja);
                ninja.EquipmentOwned.Add(muscles);
                ninja.EquipmentOwned.Add(spunk);
                context.SaveChanges();
            }
        }
 private static void DeleteNinja()
 {
     Ninja ninja;
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         ninja = context.Ninjas.FirstOrDefault();
         //context.Ninjas.Remove(ninja);
         //context.SaveChanges();
     }
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         //context.Ninjas.Attach(ninja);
         //context.Ninjas.Remove(ninja);
         context.Entry(ninja).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
        public void SaveUpdatedEquipment(NinjaEquipment equipment, int ninjaId)
        {
            using (var context = new NinjaContext())
            {
                // again, need to load existing entity as no FK in model and Ninja marked as [Required]
                // ...and again, this could be avoided by adding a NinjaId to NinjaEquipemnt!
                var equipmentWithNinjaFromDb = context
                                               .Equipment
                                               .Include(e => e.Ninja)
                                               .FirstOrDefault(e => e.Id == equipment.Id);

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

                context.SaveChanges();
            }
        }
Exemple #33
0
        private static void QueryAndUpdateNinjaDisconnected()
        {
            Ninja ninja;
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var query = context.Ninjas;
                ninja = query.FirstOrDefault();
            }

            ninja.ServedInOniwaban = !ninja.ServedInOniwaban;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Ninjas.Attach(ninja);
                context.Entry(ninja).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
Exemple #34
0
        //
        //   INSERT
        //
        private static void InsertNinja()
        {
            var ninja = new Ninja
            {
                Name = "TerriSan", ServedInOniwaban = false, DateOfBirth = new DateTime(1980,1,1), ClanId = 5
            };

            var ninja2 = new Ninja
            {
                Name = "JulieLerman",  ServedInOniwaban = false, DateOfBirth = new DateTime(1970, 1, 1), ClanId = 5
            };

            //now lets use EF to insert this object
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                //context.Ninjas.Add(ninja);
                //context.Ninjas.Add(ninja2);
                context.Ninjas.AddRange(new List<Ninja> { ninja, ninja2 });
                context.SaveChanges();
            }
        }
 private static void InsertMultipleNinjas()
 {
     var ninja1 = new Ninja
     {
         Name = "Leonardo",
         ServedInOniwaban = false,
         DateOfBirth = new DateTime(1984, 1, 1),
         ClanId = 1
     };
     var ninja2 = new Ninja
     {
         Name = "Raphael",
         ServedInOniwaban = false,
         DateOfBirth = new DateTime(1985, 1, 1),
         ClanId = 1
     };
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         context.Ninjas.AddRange(new List<Ninja> { ninja1, ninja2 });
         context.SaveChanges();
     }
 }
Exemple #36
0
 private static void InsertNinja()
 {
     var n = new Ninja
     {
         Name = "ChrisSan",
         ServedInOniwaban = false,
         DatOfBirth = new DateTime(1946, 4, 4),
         ClanId = 1
     };
     var n2 = new Ninja
     {
         Name = "BijanSan",
         ServedInOniwaban = false,
         DatOfBirth = new DateTime(1993, 4, 4),
         ClanId = 1
     };
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         context.Ninjas.Add(n);
         context.Ninjas.Add(n2);
         context.SaveChanges();
     }
 }
 public NinjaEquipment GetEquipmentById(int id)
 {
     using (var context = new NinjaContext()) {
         return(context.Equipment.Find(id));
     }
 }
Exemple #38
0
        // clears the db and reseeds with new data
        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" });

                var j = new Ninja
                {
                    Name = "JulieSan",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1980, 1, 1),
                    Clan = vtClan

                };
                var s = new Ninja
                {
                    Name = "SampsonSan",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(2008, 1, 28),
                    ClanId = 1

                };
                var l = new Ninja
                {
                    Name = "Leonardo",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1984, 1, 1),
                    Clan = turtleClan
                };
                var r = new Ninja
                {
                    Name = "Raphael",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1985, 1, 1),
                    Clan = turtleClan
                };
                context.Ninjas.AddRange(new List<Ninja> { j, s, l, r });
                var ninjaKC = new Ninja
                {
                    Name = "Kacy Catanzaro",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1990, 1, 14),
                    Clan = amClan
                };
                var muscles = new NinjaEquipment
                {
                    Name = "Muscles",
                    Type = EquipmentType.Tool,

                };
                var spunk = new NinjaEquipment
                {
                    Name = "Spunk",
                    Type = EquipmentType.Weapon
                };

                ninjaKC.EquipmentOwned.Add(muscles);
                ninjaKC.EquipmentOwned.Add(spunk);
                context.Ninjas.Add(ninjaKC);

                context.SaveChanges();
                context.Database.ExecuteSqlCommand(@"CREATE PROCEDURE GetOldNinjas AS  SELECT * FROM Ninjas WHERE DateOfBirth<='1/1/1980'");
                context.Database.ExecuteSqlCommand( @"CREATE PROCEDURE DeleteNinjaViaId @Id int AS DELETE from Ninjas Where Id = @id RETURN @@rowcount");
            }
        }
Exemple #39
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"
                });

                var j = new Ninja
                {
                    Name             = "JulieSan",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1980, 1, 1),
                    Clan             = vtClan
                };
                var s = new Ninja
                {
                    Name             = "SampsonSan",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(2008, 1, 28),
                    ClanId           = 1
                };
                var l = new Ninja
                {
                    Name             = "Leonardo",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1984, 1, 1),
                    Clan             = turtleClan
                };
                var r = new Ninja
                {
                    Name             = "Raphael",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1985, 1, 1),
                    Clan             = turtleClan
                };
                context.Ninjas.AddRange(new List <Ninja> {
                    j, s, l, r
                });
                var ninjaKC = new Ninja
                {
                    Name             = "Kacy Catanzaro",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1990, 1, 14),
                    Clan             = amClan
                };
                var muscles = new NinjaEquipment
                {
                    Name = "Muscles",
                    Type = EquipmentType.Tool,
                };
                var spunk = new NinjaEquipment
                {
                    Name = "Spunk",
                    Type = EquipmentType.Weapon
                };

                ninjaKC.EquipmentOwned.Add(muscles);
                ninjaKC.EquipmentOwned.Add(spunk);
                context.Ninjas.Add(ninjaKC);

                context.SaveChanges();
                context.Database.ExecuteSqlCommand(
                    @"CREATE PROCEDURE GetOldNinjas
                    AS  SELECT * FROM Ninjas WHERE DateOfBirth<='1/1/1980'");

                context.Database.ExecuteSqlCommand(
                    @"CREATE PROCEDURE DeleteNinjaViaId
                     @Id int
                     AS
                     DELETE from Ninjas Where Id = @id
                     RETURN @@rowcount");
            }
        }