Пример #1
0
 static Donator GetDonator(int id)
 {
     using (var db = new DonateContext())
     {
         return(db.Donator.Find(id));
     }
 }
Пример #2
0
        static void Transfer()
        {
            decimal transferAcount = 500m;

            using (var context = new DonateContext())
            {
                using (DbContextTransaction tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        string sql = @"update OutputAccount set Balance=Balance-@acount where id=@outputId";
                        context.Database.ExecuteSqlCommand(sql, new SqlParameter("@acount", transferAcount), new SqlParameter("@outputId", 1));
                        InputAccount input = context.InputAccount.Find(1);
                        input.Balance += transferAcount;
                        context.SaveChanges();
                        tran.Commit();
                        Console.WriteLine("操作成功");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("操作失败");
                        tran.Rollback();
                    }
                }
            }
        }
Пример #3
0
 static void UpdateDoonator(Donator entity)
 {
     using (var db = new DonateContext())
     {
         db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
Пример #4
0
 static void AddAccount()
 {
     using (var db = new DonateContext())
     {
         OutputAccount o = new OutputAccount();
         o.Name    = "甲";
         o.Balance = 1000;
         db.OutputAccount.Add(o);
         InputAccount input = new InputAccount();
         input.Name    = "乙";
         input.Balance = 1000;
         db.InputAccount.Add(input);
         db.SaveChanges();
     }
 }
Пример #5
0
 static void AddDonator()
 {
     using (var db = new DonateContext())
     {
         List <Donator> list = new List <Donator>();
         for (int i = 0; i < 10; i++)
         {
             Donator d = new Donator()
             {
                 Name = "Name_" + i, DonateDate = DateTime.Today, DonateTime = TypeHelper.GetTime(), Amount = 10
             };
             list.Add(d);
         }
         db.Donator.AddRange(list);
         db.SaveChanges();
     }
 }