86028

Updating record in EF 4.1

Question:

I have an employee object say:

public class Employee { public int Id {get; set;} public int Name {get; set;} public int Address {get; set;} ...other few 10's of properties }

The question is how do I only update Name? For eg. If I want to update Name I do

Employee e = Db.Employees.Where(e => e.Id == someId).SingleOrDefault(); e.Name = "Jack"; Db.SaveChanges();

As you can see, for updating I have to first get the object and then update and then call SaveChanges(). For a task that can be done in a single query to a database I have to fire of 2 queries: 1) Get the object 2) update the required object and save changes.

For traditional stored procedure approach I would just pass the Id, pass the new Name and write Update statement and I am done. Is Entity Framework really that inefficient or am I missing something?

Answer1:

You can update columns selectively:

var employee = new Employee() { Id = someId, Name = "Jack" } Db.Employees.Attach(employee); Db.Employees.Entry(employee).Property(e => e.Name).IsModified = true; Db.SaveChanges();

Note: Only EF 5 with .NET 4.5 supports setting IsModified back to false.

Answer2:

You should use <a href="http://msdn.microsoft.com/en-us/library/bb896271.aspx" rel="nofollow">Attach</a> method

var e = new Employee() { Id = someId, Name = "Jack" } Db.Employees.Attach(e); Db.Employees.Entry(e).Property(p => p.Name).IsModified = true Db.SaveChanges();

Recommend

  • Getting error in file(file, “rt”): cannot open the connection
  • According to Intel my cache should be 24-way associative though its 12-way, how is that?
  • PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
  • Customize Scrolling indicator
  • How to efficiently ensure a decimal value has at least N decimal places
  • Mercurial: Any ideas on how to skip some changesets when merging between branches?
  • How do I create a new line with reStructuredText?
  • Identifying Version of TFS via web interface
  • How to modify Azure DevOps release definition variable from a release task?
  • Send mass mail with hidden recipients
  • Can the ResourceLocalService be called remotely via the JSON-WS API?
  • Using helpers in minitest
  • Uploading files with asp.net core into database as byte array
  • How is the size of TLB in Intel's Sandy Bridge CPU determined?
  • AlertDialog before loading url in webview
  • Does Tomcat cache compiled JSP pages in memory if scratchdir is read-only?
  • Pandas: pad series on top or bottom
  • touch events not working in android phonegap webview (or even built-in browser)
  • python: Using ncurses when underlying library logs to stdout
  • import cv2 doesn't give error on command-Prompt but error on IDLE on Windows 10, Python 3.6.4
  • How to get real term_meta using ACF with custom fields on taxonomy terms (instead of wp_options)
  • How to use OPENXML to load XML data into existing SQL Table?
  • MySQL - Filter records which date is biggest
  • Using MPI_Send/Recv to handle chunk of multi-dim array in Fortran 90
  • How can I do case insensitive string search with Linq and SQL Server?
  • Validating my form with Jquery
  • DataTables move rows between tables
  • Debug `Unexpected end of JSON input Error` on content script
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Why does Rails 3 think xE2x80x89 means â x80 x89
  • Using Service Component Runtime
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • How to handle a codeigniter PDF generator