Skip to content

Instantly share code, notes, and snippets.

@paigecook
Created March 30, 2011 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paigecook/893718 to your computer and use it in GitHub Desktop.
Save paigecook/893718 to your computer and use it in GitHub Desktop.
Person Entity
public static DbContextExtensions
{
public static void EntryAsModified<T>(this DbContext context, T entity) where T : class
{
context.Entry(entity).State = EntityState.Modified;
}
public static void EntriesAsModified<T>(this DbContext context, IEnumerable<T> entities) where T : class
{
foreach(var entity in entities)
{
context.Entry(entity).State = EntityState.Modified;
}
}
}
using System.ComponentModel.DataSetAnnotations;
public class Person
{
[Key]
public int ID { get; set; }
[StringLength(100)]
public string FirstName { get; set; }
[StringLength(100)]
public string LastName { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var person = GetModifiedPersonMethod();
var context = new EntityContext();
context.Entry(person).State = EntityState.Modified;
context.SaveChanges();
}
}
using System.Data.Entity;
public class Program
{
public static void Main(string[] args)
{
var modifiedPerson = new Person
{
ID = 1,
FirstName = "John",
LastName = "Doe"
};
var context = new EntityContext();
var originalPerson = context.People.Find(1);
originalPerson.FirstName = modifiedPerson.FirstName;
originalPerson.LastName = modifiedPerson.LastName;
context.People.Attach(originalPerson);
context.SaveChanges();
}
}
public class Program
{
public static void Main(string[] args)
{
var person = GetModifiedPersonMethod();
var context = new EntityContext();
context.EntryAsModified(person);
context.SaveChanges();
var people = GetModifiedPeopleMethod();
context.EntriesAsModified(people);
context.SaveChanges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment