예제 #1
0
        public static void Delete(afmdbEntities ctx)
        {
            var w = (from e in ctx.workpiece where e.Id == 2 select e).First();

            ctx.workpiece.Remove(w);
            ctx.SaveChanges();
        }
예제 #2
0
 private static void Insert(afmdbEntities ctx)
 {
     ctx.workpiece.Add(new workpiece {
         StartTime = DateTime.Now
     });
     ctx.SaveChanges();
 }
예제 #3
0
        public static void Update(afmdbEntities ctx)
        {
            var w = (from e in ctx.workpiece where e.Id == 2 select e).First();

            w.Name = "aaa";
            ctx.SaveChanges();
        }
예제 #4
0
        public static void TransactionScope()
        {
            using (var ctx = new afmdbEntities())
            {
                using (var scope = new TransactionScope())
                {
                    List <workpiece> workpieces = new List <workpiece>();
                    for (int i = 0; i < 5; i++)


                    {
                        workpieces.Add(new workpiece {
                            Name = string.Format("www{0}", i), StartTime = DateTime.Now
                        });
                    }
                    ctx.workpiece.AddRange(workpieces);
                    ctx.SaveChanges();
                    scope.Complete();
                }
            }
        }
예제 #5
0
        public static void Transaction()
        {
            // Create database if not exists
            using (afmdbEntities ctx = new afmdbEntities())
            {
                ctx.Database.CreateIfNotExists();
            }

            using (afmdbEntities ctx = new afmdbEntities())
            {
                // Interception/SQL logging
                ctx.Database.Log = (string message) => { Console.WriteLine(message); };

                using (var transaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        List <workpiece> workpieces = new List <workpiece>();
                        for (int i = 0; i < 5; i++)
                        {
                            workpieces.Add(new workpiece {
                                Name = string.Format("www{0}", i), StartTime = DateTime.Now
                            });
                        }
                        ctx.workpiece.AddRange(workpieces);
                        ctx.SaveChanges();
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }