2011-02-18 16 views
1

我有一個類:在具有實體框架的提供者上使用哪種方法?

public class Tool 
{ 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public Int64 ID { get; set; } 

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public string Name { get; set; } 
} 

我的供應商有兩種方法:

public IEnumerable<Tool> GetFirst() 
{ 
    using (var db = new Entitites()) 
    { 
     return db.Tools.FirstOrDefault(); 
    } 
} 

public void Update(Tool o) 
{ 
    using (var db = new Entities()) 
    { 
     db.Tools.SaveChanges(); 
    } 
} 

它不工作,因爲他們都在不同的情況下,甚至沒有被用在Update方法的參數。但是,我可以從數據庫中獲取對象,並使用參數對象逐個更改字段,然後保存更改。

我該怎麼辦?

  • 更新對象並保存?
  • 保持提供者只有一個上下文?
  • 另一種方法?
+0

使用同樣的背景下兩種方法。這樣做有什麼挑戰嗎? – 2011-02-18 17:19:44

回答

0

我發現從another questionattach方法,神似

using (var db = new Entitites()) 
{ 
    // Attach the object on this context 
    db.Attach(tool); 

    // Change the state of the context to ensure update 
    db.ObjectStateManager.GetObjectStateEntry(tool).SetModified(); 

    // ClientWins, flawless victory 
    db.Refresh(RefreshMode.ClientWins, tool); 
} 
相關問題