コード例 #1
0
 public static void Insert()
 {
     try
     {
         using (var context = new EFTestModel())
         {
             context.Customers.Add(new Customer
             {
                 CustomerID = 2,
                 FirstName  = "Karthik ",
                 LastName   = "Maha"
             });
             //context.SaveChanges();
             context.CustomerDetails.Add(new CustomerDetails
             {
                 CustomerID = 2,
                 Email      = "*****@*****.**",
                 Address    = "Dindual"
             });
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
コード例 #2
0
 public static void Insert()
 {
     try
     {
         using (var context = new EFTestModel())
         {
             using (var dbcontextTransaction = context.Database.BeginTransaction())
             {
                 try
                 {
                     context.Customers.Add(new Customer
                     {
                         FirstName = "Latha ",
                         LastName  = "Aamu"
                     });
                     context.SaveChanges();
                     context.CustomerDetails.Add(new CustomerDetails
                     {
                         CustomerID = 10,
                         Email      = "*****@*****.**",
                         Address    = "Aamu"
                     });
                     context.SaveChanges();
                     dbcontextTransaction.Commit();
                 }
                 catch (Exception ex)
                 {
                     dbcontextTransaction.Rollback();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }