示例#1
0
        static void DeleteCustomersViaTrx()

        {
            using (var db = new ShoppingContext())

                using (var transaction = db.Database.BeginTransaction())

                {
                    var customers = db.Customers.Include(b => b.Orders).ToList();
                    foreach (var customer in customers)
                    {
                        db.Entry(customer).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;

                        int recordsDeleted = db.SaveChanges();

                        Console.WriteLine("Number of records deleted:" + recordsDeleted);
                    }

                    transaction.Commit();
                }
        }
示例#2
0
        private static async Task AddOrders()
        {
            using (var db = new ShoppingContext())
            {
                var customer1 = new Customer()
                {
                    CustomerName = "Onur"
                };
                var customer2 = new Customer()
                {
                    CustomerName = "Frisk"
                };

                customer1.Orders.Add(new Order()
                {
                    Item = "Beginning ASP.NET", Price = 35.55M
                });
                db.Customers.Add(customer1);
                db.Customers.Add(customer2);
                await db.SaveChangesAsync();
            }
        }