示例#1
0
 private static void UpdateUsingSqlCommand()
 {
     using (SamuriDbContext context = new SamuriDbContext())
     {
         int count = context.Database.ExecuteSqlCommand("Update Samuris set Name=REPLACE(Name, 'San', 'Nan')");
         Console.WriteLine($"count = {count}");
     }
 }
示例#2
0
        private static void QuerySamuris()
        {
            using (SamuriDbContext context = new SamuriDbContext())
            {
                List <Samuri> samuris = context.Samuris.Where(s => s.Name.StartsWith("P")).ToList();

                foreach (var samuri in samuris)
                {
                    Console.WriteLine(samuri.Name);
                }
            }
        }
示例#3
0
        private static void InsertSamuris()
        {
            Samuri samuri = new Samuri {
                Name = "Frank"
            };

            using (SamuriDbContext context = new SamuriDbContext())
            {
                context.Samuris.Add(samuri);
                context.SaveChanges();
            }
        }
示例#4
0
        private static void RetrieveAndUpdateMultipleSamuri()
        {
            using (SamuriDbContext context = new SamuriDbContext())
            {
                List <Samuri> samuris = context.Samuris.ToList();

                if (samuris.Count > 0)
                {
                    samuris.ForEach(s => s.Name += "San");
                    context.SaveChanges();
                }
            }
        }
示例#5
0
        private static void RetrieveAndUpdateSamuri()
        {
            using (SamuriDbContext context = new SamuriDbContext())
            {
                Samuri samuri = context.Samuris.FirstOrDefault();

                if (samuri == null)
                {
                    return;
                }

                samuri.Name += "San";
                context.SaveChanges();
            }
        }
示例#6
0
        private static void QueryUsingRawSql()
        {
            using (SamuriDbContext context = new SamuriDbContext())
            {
                List <Samuri> samuris = context.Samuris.FromSql("Select * from Samuris").ToList();

                if (samuris.Count <= 0)
                {
                    return;
                }

                foreach (var samuri in samuris)
                {
                    Console.WriteLine(samuri.Name);
                }
            }
        }
示例#7
0
        private static void InsertMultipleSamuris()
        {
            Samuri samuri1 = new Samuri {
                Name = "Peter"
            };
            Samuri samuri2 = new Samuri {
                Name = "Paul"
            };
            List <Samuri> samuriList = new List <Samuri> {
                samuri1, samuri2
            };

            using (SamuriDbContext context = new SamuriDbContext())
            {
                context.Samuris.AddRange(samuriList);
                context.SaveChanges();
            }
        }