示例#1
0
 private static void CreateDatabase()
 {
     using (var context = new PaymentsContext())
     {
         context.Database.EnsureCreated();
         context.Payments.Add(new CashPayment {
             Amount = 0.2M, Name = "Donald"
         });
         context.Payments.Add(new CashPayment {
             Amount = 100000M, Name = "Scrooge"
         });
         context.Payments.Add(new CreditcardPayment {
             Amount = 500, Name = "Gladstone", CreditcardNumber = "08154711123"
         });
         int changed = context.SaveChanges();
         Console.WriteLine($"changed {changed} records");
     }
 }
示例#2
0
        private static void Query()
        {
            using (var context = new PaymentsContext())
            {
                var payments = context.Payments.ToList();
                foreach (var payment in payments)
                {
                    Console.WriteLine($"{payment.Name} {payment.Amount} {payment.GetType().Name}");
                }

                Console.WriteLine();
                Console.WriteLine("just the creditcard payments");
                foreach (var payment in context.Payments.OfType <CreditcardPayment>())
                {
                    Console.WriteLine($"{payment.Name} {payment.Amount} {payment.GetType().Name}");
                }
            }
        }