2011-02-18 55 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); 
} 
相关问题