开发者

how to update an entity in Entity Framework 4 .NET

my code is something like this:

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?
 }
}

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)


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) 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) 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();
    }
  } 
}


Taken from Employee Info Starter Kit, you can consider the code snippet as below:

public void UpdateEmployee(Employee updatedEmployee)
        {
            //attaching and making ready for parsistance
            if (updatedEmployee.EntityState == EntityState.Detached)
                _DatabaseContext.Employees.Attach(updatedEmployee);
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
            _DatabaseContext.SaveChanges();
        }


does not work when you have a property on entity which is a ConcurrencyToken. At least for me. Because you then get a OptimisticConcurrencyException.

What i do (and i think this is not an optimum solution),

facts: - I use a new context because of n-tier. So, the previous/original entity with its values are not known. Either you supplies the context with original and old (bah) or like me load original first prior to update:

T originalItem = sessionManager.Set().Single(x => x.ID == changedEntity.ID);

        if(changedEntity.lastChangedDate != originalItem.lastChangedDate)
            throw new OptimisticConcurrencyException(String.Format("Trying to update entity with lastChangedDate {0} using lastChangedDate {1}!", originalItem.lastChangedDate, changedEntity.lastChangedDate));

        ObjectStateEntry state = sessionManager.ObjectStateManager.GetObjectStateEntry(originalItem);
        state.ApplyCurrentValues(changedEntity);
        state.ChangeState(System.Data.EntityState.Modified);
        sessionManager.SaveChanges();

If you know something better, please let me know.

Atam

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜