Example #1
0
        static void CreateCustomer(string name)
        {
            using (var ctx = new TestEFDBFirstContext())
            {
                var customer = new Customer()
                {
                    Name = name
                };

                ctx.Customers.Add(customer);
                ctx.SaveChanges();
            }
        }
Example #2
0
        static void GetCustomers()
        {
            Console.WriteLine("List of Customers...");
            using (var ctx = new TestEFDBFirstContext())
            {
                var customers = ctx.Customers.ToList <Customer>();

                foreach (var cust in customers)
                {
                    Console.WriteLine($"{cust.Id}: {cust.Name}");
                }
            }
        }
Example #3
0
 static void DeleteCustomer(int id)
 {
     using (var ctx = new TestEFDBFirstContext())
     {
         var customer = ctx.Customers
                        .Where(c => c.Id == id)
                        .SingleOrDefault <Customer>();
         if (customer != null)
         {
             ctx.Customers.Remove(customer);
             ctx.SaveChanges();
         }
     }
 }
Example #4
0
        static void UpdateCustomer(int id, string name)
        {
            using (var ctx = new TestEFDBFirstContext())
            {
                var customer = ctx.Customers
                               .Where(c => c.Id == id)
                               .SingleOrDefault <Customer>();

                if (customer != null)
                {
                    customer.Name             = name;
                    ctx.Entry(customer).State = System.Data.Entity.EntityState.Modified;

                    ctx.SaveChanges();
                }
            }
        }