Created
March 30, 2011 01:49
-
-
Save paigecook/893718 to your computer and use it in GitHub Desktop.
Person Entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var person = GetModifiedPersonMethod(); | |
var context = new EntityContext(); | |
context.Entry(person).State = EntityState.Modified; | |
context.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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