Example #1
0
        private static void QueryAndUpdateNinja()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = context.Ninjas.First();

                ninja.ServedInOniwaban = !ninja.ServedInOniwaban;
                context.SaveChanges();
            }
        }
Example #2
0
 private static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         var ninja = context.Ninjas
                     .Where(n => n.DateOfBirth >= new DateTime(1984, 1, 1))
                     .OrderBy(n => n.Name)
                     .Skip(1).Take(1)
                     .FirstOrDefault();
         Console.WriteLine(ninja.Name);
     }
 }
Example #3
0
 private static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         var ninja = context.Ninjas.Where(n => n.DateOfBirth >= new DateTime(1984, 1, 1))
                     .FirstOrDefault();
         //foreach (Ninja ninja in ninjas)
         //{
         Console.WriteLine(ninja.Name);
         //}
     }
 }
Example #4
0
        private static void DeleteNinjaViaStoredProcedure()
        {
            var keyval = 4;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = context.Ninjas.Find(keyval);
                context.Database.ExecuteSqlCommand(
                    "exec DeleteNinjaViaId {0}", keyval);
            }
        }
Example #5
0
 private static void simpleNinjaGraphQuery()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninja = context.ninjas.FirstOrDefault(n => n.name.StartsWith("Ab"));
         Console.WriteLine(ninja.name);
         // Explicite loading
         context.Entry(ninja).Collection(n => n.equipementOwned).Load();
         // add virtual to the field if you a lazy loading
     }
 }
Example #6
0
        private static void DeleteNinjaWithKeyValue()
        {
            var keyval = 1;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = context.Ninjas.Find(keyval); //DB round trip #1
                context.Ninjas.Remove(ninja);
                context.SaveChanges();                   //DB round trip #2
            }
        }
Example #7
0
 private static void RetrieveDataWithStoredProc()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninjas = context.Ninjas.SqlQuery("exec GetOldNinjas");
         foreach (var ninja in ninjas)
         {
             Console.WriteLine(ninja.Name);
         }
     }
 }
Example #8
0
        private static void ReseedDatabase()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <NinjaContext>());
            using (NinjaContext context = new NinjaContext())
            {
                context.Clans.Add(new Clan {
                    ClanName = "Vermont Clan"
                });
                Ninja j = new Ninja
                {
                    Name             = "JulieSan",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1980, 1, 1),
                    ClanId           = 1
                };
                Ninja s = new Ninja
                {
                    Name             = "SampsonSan",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(2008, 1, 28),
                    ClanId           = 1
                };
                Ninja l = new Ninja
                {
                    Name             = "Leonardo",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1984, 1, 1),
                    ClanId           = 1
                };
                Ninja r = new Ninja
                {
                    Name             = "Raphael",
                    ServedInOniwaban = false,
                    DateOfBirth      = new DateTime(1985, 1, 1),
                    ClanId           = 1
                };
                context.Ninjas.AddRange(new List <Ninja> {
                    j, s, l, r
                });
                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");
            }
        }
Example #9
0
        private static void QueryNinjaWithEquipment3()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = context.Ninjas
                            .FirstOrDefault(n => n.Name.StartsWith("Raphel"));

                // lazy loading: if EquipmentOwned is virtual, entity will go back to the db to get EquipmentOwned when it is referenced
                Console.WriteLine("Ninja EquipmentCount: {0}", ninja.EquipmentOwned.Count());
            }
        }
Example #10
0
        private static void QueryNinjaWithEquipment2()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = context.Ninjas
                            .FirstOrDefault(n => n.Name.StartsWith("Raphel"));

                // if there was more than one result we could just go back and get the equipment for this one
                context.Entry(ninja).Collection(n => n.EquipmentOwned).Load();
            }
        }
Example #11
0
 private static void InsertClans()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         context.Clans.Add(new Clan()
         {
             Id       = 1,
             ClanName = "Adventures"
         });
         context.SaveChanges();
     }
 }
Example #12
0
 private static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninjas = context.Ninjas.Where(n => n.Name.Contains("Shamma"));
         foreach (var ninja in ninjas)
         {
             Console.WriteLine(ninja.Name);
         }
         //var query = context.Ninjas;
     }
 }
        private static void QueryAndUpdateNinja()
        {
            using (NinjaContext context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                Ninja ninja = context.Ninjas.FirstOrDefault();

                // update the instance will create a corresponding
                // update for sql.
                ninja.ServedInOniwaban = (!ninja.ServedInOniwaban);
                context.SaveChanges();
            }
        }
Example #14
0
        static void  RetrieveDataWithFind(int keyval)
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = context.Ninjas.Find(keyval);
                Console.WriteLine("Find#1:" + ninja.Name);

                var someNinja = context.Ninjas.Find(keyval);
                Console.WriteLine("Find#2" + someNinja.Name);
                ninja = null;
            }
        }
Example #15
0
        static void QueryAndUpdateNinja()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = context.Ninjas.FirstOrDefault();
                ninja.ServedInOniwaban = true;

                //context.Entry(ninja).State = EntityState.Modified; //executes an update for all columns, does not check changes

                context.SaveChanges();
            }
        }
Example #16
0
        public static void CreateClan()
        {
            var clan = new Clan
            {
                ClanName = "ZOLWIE",
            };

            using (var context = new NinjaContext())
            {
                context.Clans.Add(clan);
                context.SaveChanges();
            }
        }
Example #17
0
 private static void RetrieveDataWithStoredProc()
 {
     //CREATE PROCEDURE GetOldNinjas AS SELECT * FROM Ninjas WHERE DateOfBirth <= '1/1/1980'
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         var ninjas = context.Ninjas.SqlQuery("EXEC GetOldNinjas");
         foreach (var ninja in ninjas)
         {
             Console.WriteLine(ninja.Name);
         }
     }
 }
Example #18
0
        private static void SimpleNinjaGraphQuery_LazyLoading()
        {
            //NOTE: To enable lazy loading for the EquipmentOwned property on the Ninja class the property must be declared with public virtual
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = context.Ninjas
                            .FirstOrDefault(n => n.Name.StartsWith("Kacy"));
                Console.WriteLine("Ninja Retrieved: " + ninja.Name);
                Console.WriteLine("Ninja Equipment Count: {0}", ninja.EquipmentOwned.Count());
            }
        }
Example #19
0
        private static void SimpleNinjaGraphQuery_ExplicitLoading()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var ninja = context.Ninjas
                            .FirstOrDefault(n => n.Name.StartsWith("Kacy"));
                Console.WriteLine("Ninja Retrieved: " + ninja.Name);
                //context.Entry(ninja).Collection(n => n.EquipmentOwned).Load();
                Console.WriteLine("Ninja Equipment Count: {0}", ninja.EquipmentOwned.Count());
            }
        }
Example #20
0
 private static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         var ninjas = context.Ninjas.ToList();
         // Never iterate directly over DB context, bcuz transaction stays open
         // as long as u iterate
         //foreach(var ninja in context.Ninjas)
         //{
         //    // ...
         //}
     }
 }
Example #21
0
        }//InsertMultipleNinjas()

        private static void SimpleNinjaQueries()
        {
            using (var context = new NinjaContext())
            {
                var ninjas = context.Ninjas.Where(n => n.Name == "Raphael");
                //var query = context.Ninjas;
                //var someninjas = query.ToList();
                foreach (var ninja in ninjas)
                {
                    Console.WriteLine(ninja.Name);
                } //query
            }
        }         //SimpleNinjaQueries()
Example #22
0
 private static void RetrieveDataWithStoredProc()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         //uses stored proc to query and find ninjaName
         var ninjas = context.Ninjas.SqlQuery("exec GetOldNinjas"); //can also add .tolist()
         foreach (var ninja in ninjas)
         {
             Console.WriteLine(ninja.Name);
         }
     }
 }
        private static void SimpleNinjaGraphQuery()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                // Use DbSet.Include to auto join related data in another table

                var ninja = context.Ninjas
                            .Include(n => n.EquipmentOwned)
                            .FirstOrDefault(n => n.Name.StartsWith("Kacy"));
            }
        }
Example #24
0
        private static void SimpleNinjaQuery()
        {
            using (var context = new NinjaContext())
            {
                var ninjas = context.Ninjas.OrderBy(n => n.Name)
                             .Skip(1).Take(2).ToList();

                foreach (var ninja in ninjas)
                {
                    Console.WriteLine(ninja.Name);
                }
            }
        }
Example #25
0
 private static void RetriveDataWithFind()
 {
     using (var context = new NinjaContext())
     {
         // retrieves row with id of value 7
         // Find check if the object already exists in the mmemory and is tracked by the context
         // if yes it doesnt query db
         var keyval = 7;
         context.Database.Log = Console.WriteLine;
         var ninja = context.Ninjas.Find(keyval);
         Console.WriteLine(ninja.Name);
     }
 }
Example #26
0
        private static void deleteNinjaUsingStoreProcedure()
        {
            int valkey = 4;

            using (var context = new NinjaContext())
            {
                /*
                 * sqlquery vs ExecuteSqlCommand
                 * - sqlquery is query your DBSet and return entities from database
                 * - Execute is query context.Database and it run cmd and return status code
                 */
                context.Database.ExecuteSqlCommand("exec yourStoreProcedure {0}", valkey);
            }
        }
Example #27
0
        private static void RetrieveDataWithFind()
        {
            var keyVal = 3;

            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                var ninja = context.Ninjas.Find(keyVal);
                Console.WriteLine("After Find #1 " + ninja.Name);

                var ninjaAgain = context.Ninjas.Find(keyVal);
                Console.WriteLine("After Find #2 " + ninjaAgain.Name);
            }
        }
Example #28
0
        /// <summary>
        /// Simple find and update method
        /// </summary>
        private static void SimpleQueryAndUpdate()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                var results = context.Ninjas.FirstOrDefault();
                if (results != null)
                {
                    results.DateOfBirth = DateTime.Today.AddYears(-25);
                }
                context.SaveChanges();
            }
        }
Example #29
0
 private static void RetriveDataWithStoredProc()
 {
     using (var context = new NinjaContext())
     {
         context.Database.Log = Console.WriteLine;
         //SqlQuery is a method of DBSet
         // result db schema must match the schema of the type
         var ninjas = context.Ninjas.SqlQuery("exec GetOldNinjas");
         foreach (var ninja in ninjas)
         {
             Console.WriteLine(ninja.Name);
         }
     }
 }
Example #30
0
 public static void SimpleNinjaQueries()
 {
     using (var context = new NinjaContext())
     {
         var query = context.Ninjas.ToList();
         foreach (var item in query)
         {
             Console.WriteLine("Name:" + item.Name);
             Console.WriteLine("ServedInOniwaban:" + item.ServedInOniwaban);
             Console.WriteLine("ClanId:" + item.ClanId);
             Console.WriteLine("-----------------------");
         }
     }
 }