Skip to content

Instantly share code, notes, and snippets.

@lancscoder
Created February 14, 2012 19:28
Show Gist options
  • Save lancscoder/1829463 to your computer and use it in GitHub Desktop.
Save lancscoder/1829463 to your computer and use it in GitHub Desktop.
Entity Framework Getting Started
// Entity Framework - Delete
using (var db = new BlogContext()) {
var post = db.Posts.Find(1);
db.Posts.Remove(post);
db.SaveChanges();
}
// Entity Framework – Filtered List
using (var db = new BlogContext()) {
var posts = db.Posts.Where(p => p.Text.Contains("Some Value")).ToList();
}
// Entity Framework - Insert
using (var db = new BlogContext()) {
var post = new Post { 
Title = "Title 1", 
Text = "Text 1", 
PublishDate = DateTime.Now };
db.Posts.Add(post);
db.SaveChanges();
}
// Entity Framework – Simple List
using (var db = new BlogContext()) {
var posts = db.Posts.ToList();
}
// Entity Framework - Single
using (var db = new BlogContext()) {
var post = db.Posts.Find(1);
}
// Entity Framework - Update
using (var db = new BlogContext()) {
var post = db.Posts.Find(1);
post.Title = "New Title";
db.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment