2012-06-17 65 views
-3

我在我的項目中使用實體框架5,我想更新記錄。我該怎麼做呢?實體框架更新

這是我的基類。

using System; 

namespace EF_Sample09.DomainClasses 
{ 
    public abstract class BaseEntity 
    { 
     public int Id { get; set; } 

     public DateTime CreatedOn { set; get; } 
     public string CreatedBy { set; get; } 

     public DateTime ModifiedOn { set; get; } 
     public string ModifiedBy { set; get; } 
    } 
} 
+1

我假設你想要一個如何使用EF執行更新的例子? –

+0

no.iwant更新數據 – Mohammad

回答

0

從大ADO.NET Entity Framework Overview考慮:

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()方法將這些更改寫入到數據庫

這就是它!