偉大なADO.NET Entity Framework の概要から取得:
using(AdventureWorksDB aw = new
AdventureWorksDB(Settings.Default.AdventureWorks)) {
// find all people hired at least 5 years ago
Query<SalesPerson> oldSalesPeople = aw.GetQuery<SalesPerson>(
"SELECT VALUE sp " +
"FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp " +
"WHERE sp.HireDate < @date",
new QueryParameter("@date", DateTime.Today.AddYears(-5)));
foreach(SalesPerson p in oldSalesPeople) {
// call the HR system through a webservice to see if this
// sales person has a promotion coming (note that this
// entity type is XML-serializable)
if(HRWebService.ReadyForPromotion(p)) {
p.Bonus += 10; // give a raise of 10% in the bonus
p.Title = "Senior Sales Representative"; // give a promotion
}
}
// push changes back to the database
aw.SaveChanges();
}
基本的に次のことを行う必要があります。
ObjectContext
あなたの(またはDbContext
)を作成します
- いくつかのレコードを取得する
- オブジェクトを変更する
- コンテキストの
.SaveChanges()
メソッドを呼び出して、これらの変更をデータベースに書き戻します
それでおしまい!