コード例 #1
0
ファイル: Program.cs プロジェクト: Hekotoru/Testing-EF6
        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();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: sgccarey/EFNinjas
        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 void DeleteNinja(int ninjaId)
 {
     using (var context = new NinjaContext()) {
     var ninja = context.Ninjas.Find(ninjaId);
     context.Entry(ninja).State = EntityState.Deleted;
     context.SaveChanges();
       }
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: Hekotoru/Testing-EF6
 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();
     }
 }
コード例 #5
0
ファイル: Program.cs プロジェクト: toddbadams/FunWithEf
 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();
     }
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: sbollap1/NinjaDemoProject
 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();
     }
 }
コード例 #7
0
 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();
     }
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: Hekotoru/Testing-EF6
        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();
            }
        }
コード例 #9
0
 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();
     }
 }
コード例 #10
0
ファイル: Program.cs プロジェクト: toddbadams/FunWithEf
        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();
            }
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: sbollap1/NinjaDemoProject
        //
        //   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();
            }
        }
コード例 #12
0
 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();
     }
 }
コード例 #13
0
ファイル: Program.cs プロジェクト: toddbadams/FunWithEf
 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 void SaveNewNinja(Ninja ninja)
 {
     using (var context = new NinjaContext()) {
     context.Ninjas.Add(ninja);
     context.SaveChanges();
       }
 }
コード例 #15
0
ファイル: Program.cs プロジェクト: sgccarey/EFNinjas
        private static void InsertNinja()
        {
            var ninja = new Ninja
            {
                Name = "PenelopeSan",
                ServedInOniwaban = false,
                DateOfBirth = new DateTime(2015, 01, 08),
                ClanId = 1
            };

            using (var context = new NinjaContext())
            {
                // point the database log function to Console, so we can see the output
                context.Database.Log = Console.WriteLine;

                // Calls sql INSERT function - INSERT [dbo].[Ninjas]([Name], [ServedInOniwaban], [ClanId], [DateOfBirth])
                context.Ninjas.Add(ninja);

                // This must be called, or changes to the context will not be applied to the actual db
                context.SaveChanges();
            }
        }
        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();
              }
        }
        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();
              }
        }
 public void SaveUpdatedNinja(Ninja ninja)
 {
     using (var context = new NinjaContext()) {
     context.Entry(ninja).State = EntityState.Modified;
     context.SaveChanges();
       }
 }
コード例 #19
0
ファイル: Program.cs プロジェクト: sgccarey/EFNinjas
        private static void QueryAndUpdateNinja()
        {
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;

                // first call to db: SELECT TOP 1 FROM Ninjas
                var ninja = context.Ninjas.FirstOrDefault();

                // context modified, not db
                ninja.ServedInOniwaban = !ninja.ServedInOniwaban;

                // second call to db: UPDATE Ninjas SET ServedInOniwaban = 0 WHERE Id = 1
                context.SaveChanges();
            }
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: sbollap1/NinjaDemoProject
        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();
            }
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: sbollap1/NinjaDemoProject
        private static void QueryAndUpdateNinjaDisconnected()
        {
            Ninja n;
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                n = context.Ninjas.FirstOrDefault();
            }

            n.ServedInOniwaban = (!n.ServedInOniwaban);

            //Notice you have a new context so its not aware of any tracking
            //Hence you can use Add, but that will add regardless
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                //context.Ninjas.Add(n); //This is add regadless though
                context.Ninjas.Attach(n);
                context.Entry(n).State = EntityState.Modified;  //But that will pass all the values
                context.SaveChanges();
            }
        }
コード例 #22
0
ファイル: Program.cs プロジェクト: sgccarey/EFNinjas
        private static void QueryAndUpdateNinjaDisconnected()
        {
            Ninja ninja;

            // represents a service or WebAPI retrieving the data and sending it to the client
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                ninja = context.Ninjas.FirstOrDefault();
            }

            // client modifies data
            ninja.ServedInOniwaban = (!ninja.ServedInOniwaban);

            // reinstantiate the context and call save changes
            // completely new context, not aware of ninja, so won't save changes
            using (var context = new NinjaContext())
            {
                context.Database.Log = Console.WriteLine;
                // add the ninja to the context to fix above problem

                // this doesn't look for a matching ID and update, it will INSERT a new row
                //context.Ninjas.Add(ninja);

                // tells EF to watch this data
                context.Ninjas.Attach(ninja);

                // tell the context that the entity has changed, and the object should be updated in the DB
                context.Entry(ninja).State = EntityState.Modified;

                context.SaveChanges();
            }
        }
コード例 #23
0
ファイル: Program.cs プロジェクト: sgccarey/EFNinjas
        // used to prepare db for part 4
        private static void ReseedDatabase()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<NinjaContext>());
            using (var context = new NinjaContext())
            {
                context.Clans.Add(new Clan { ClanName = "Vermont Clan" });
                var j = new Ninja
                {
                    Name = "JulieSan",
                    ServedInOniwaban = false,
                    DateOfBirth = new DateTime(1980, 1, 1),
                    ClanId = 1

                };
                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),
                    ClanId = 1
                };
                var 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");
            }
        }
コード例 #24
0
 public void Save()
 {
     RemoveEmptyNewNinjas();
     _context.SaveChanges();
 }
コード例 #25
0
ファイル: Program.cs プロジェクト: joseph-ortiz/EF6-Demo
        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();

            }
        }
コード例 #26
0
ファイル: DataHelpers.cs プロジェクト: sgccarey/EFNinjas
        // 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");
            }
        }
コード例 #27
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");
            }
        }