如何在 Entity Framework 4 .NET 中更新实体 [英] how to update an entity in Entity Framework 4 .NET

查看:50
本文介绍了如何在 Entity Framework 4 .NET 中更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的:

public class Program
{
 [STAThread]
 static void main()
 {
  DataAccessClass dal = new DataAccessClass();
  List<Person> list = dal.GetPersons();
  Person p = list[0];
  p.LastName = "Changed!";
  dal.Update(p);
 }
}

public class DataAccessClass
{
 public static List<Person> GetPersons()
 {
  MyDBEntities context = new MyDBEntities();
  return context.Persons.ToList();
 }

 public void Update(Person p)
 {
  // what sould be written here?
 }
}

现在请告诉我应该在 Update() 方法中写什么?我写的一切,都会遇到各种异常.(请注意加载的数据是否被跟踪、连接或类似)

now please tell me what should i write in the Update() method? everything i write , encounters various exceptions. (please pay attention that the data loaded is tracked , connected or something like that)

推荐答案

问题是您的 Person 实体仍然附加到在 GetPersons 中创建的上下文.如果要使用附加实体,则必须在选择和更新操作中使用相同的上下文实例.您有两种选择来解决您的问题.

The problem is that your Person entities are still attached to context created in GetPersons. If you want to work with attached entities you have to use same context instance in both select and update operations. You have two choices to solve your problem.

1) 正确处理附加实体

1) Correctly handled attached entities

public class Program 
{ 
  [STAThread] 
  static void main() 
  { 
    using (DataAccessClass dal = new DataAccessClass())
    {
      List<Person> list = dal.GetPersons(); 
      Person p = list[0]; 
      p.LastName = "Changed!"; 
      dal.Save();
    } 
  } 
} 

public class DataAccessClass : IDisposable
{ 
  private MyDBEntities _context = new MyDBEntities(); 

  public List<Person> GetPersons() 
  { 
    return _context.Persons.ToList(); 
  } 

  public void Save() 
  { 
    // Context tracks changes on your entities. You don't have to do anything. Simply call
    // SaveChanges and all changes in all loaded entities will be done in DB.
    _context.SaveChanges();
  } 

  public void Dispose()
  {
    if (_context != null)
    {
      _context.Dispose();
      _context = null;
    }
  }
}

2) 不要使用附加实体

2) Don't use attached entities

public class Program 
{ 
  [STAThread] 
  static void main() 
  { 
    DataAccessClass dal = new DataAccessClass())
    List<Person> list = DataAccessClass.GetPersons(); 
    Person p = list[0]; 
    p.LastName = "Changed!"; 
    dal.Update(p);
  } 
} 

public class DataAccessClass
{ 
  public static List<Person> GetPersons() 
  {
    // Closing context will detach entities
    using (MyDBEntities context = new MyDBEntities())
    { 
      return context.Persons.ToList(); 
    }
  } 

  public void Update(Person p) 
  {  
    using (MyDBEntities context = new MyDBEntities())
    {
      context.Persons.Attach(p);
      // Detached entities don't track changes so after attaching you have to say
      // what changes have been done
      context.ObjectStateManager.ChangeObjectState(p, System.Data.EntityState.Modified);
      context.SaveChanges();
    }
  } 
}

这篇关于如何在 Entity Framework 4 .NET 中更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆