Skip to content

Instantly share code, notes, and snippets.

@nozzlegear
Created April 22, 2014 15:36
Show Gist options
  • Save nozzlegear/11183928 to your computer and use it in GitHub Desktop.
Save nozzlegear/11183928 to your computer and use it in GitHub Desktop.
Updating all properties on an entity using Entity Framework
//Open database context
using(DbContext db = new DbContext())
{
//Get the id of the entity we want to update
int id = 1;
//Create an instance of the entity we want to update
SomeEntity entity = new SomeEntity(){
Id = id,
Email = "[email protected]",
Name = "Joshua Harms",
Birthady = "July 5th"
};
//Attach the entity to the context
db.YourDbSet.Attach(entity);
//Now set the entity's entire state to modifed
db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
//Save the changes to the dbcontext
db.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment