ADO.net Entity Framework 示例代码

查询
 AccountBookEntities context = new AccountBookEntities();
ObjectQuery
<Payment> query = context.CreateQuery<Payment>("AccountBookEntities.Payment");
ObjectResult
<Payment> result = query.Execute(MergeOption.NoTracking);

this.dataGridView1.DataSource = result;

  

增加   
AccountBookEntities context = new AccountBookEntities();

Payment pay
= new Payment();
//pay.PaymentID = 8;
pay.PaymentDate = DateTime.Now;
pay.PaymentMoney
= 100;
pay.PaymentContent
= "测'试";

context.AddToPayment(pay);

context.SaveChanges();

  

删除 
AccountBookEntities context = new AccountBookEntities();

var query
= from pay in context.Payment
where pay.PaymentID == 2
select pay;

foreach (Payment pay in query)
{
context.DeleteObject(pay);
}

context.SaveChanges();

  

修改  
AccountBookEntities context = new AccountBookEntities();

var query
= from pay in context.Payment
where pay.PaymentID == 1
select pay;

foreach (Payment pay in query)
{
pay.PaymentContent
= "修改后";
}

context.SaveChanges();

  

posted @ 2011-08-19 16:56  liming1019  阅读(229)  评论(0编辑  收藏  举报