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(); }
private static void Insert(afmdbEntities ctx) { ctx.workpiece.Add(new workpiece { StartTime = DateTime.Now }); ctx.SaveChanges(); }
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(); }
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(); } } }
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; } } } }