Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.77 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. why EF tries to add new record instead of edit?
  2. var currentUser = (from i in _dbContext.Users
  3.                            where i.FirstName == user.FirstName && i.LastName == user.LastName
  4.                                && i.Title == user.Title && i.Company == user.Company
  5.                            select i).FirstOrDefault();
  6.  
  7.                         currentUser.Company = user.Company;
  8.                         currentUser.CompanyUrl = user.CompanyUrl;
  9.                         currentUser.Country = user.Country;
  10.  
  11.                         _dbContext.SaveChanges();
  12.        
  13. currentUser.Company = user.Company;
  14.        
  15. var currentUser = ...;
  16.  
  17. var company = new Company { Id = user.Company.Id };
  18. _dbContext.Companies.Attach(company);
  19.  
  20. currentUser.Company = company;
  21. ...
  22.  
  23. _dbContext.SaveChanges();