Created
February 14, 2012 19:28
-
-
Save lancscoder/1829463 to your computer and use it in GitHub Desktop.
Entity Framework Getting Started
This file contains 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
// Entity Framework - Delete | |
using (var db = new BlogContext()) { | |
var post = db.Posts.Find(1); | |
db.Posts.Remove(post); | |
db.SaveChanges(); | |
} |
This file contains 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
// Entity Framework – Filtered List | |
using (var db = new BlogContext()) { | |
var posts = db.Posts.Where(p => p.Text.Contains("Some Value")).ToList(); | |
} |
This file contains 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
// 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(); | |
} |
This file contains 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
// Entity Framework – Simple List | |
using (var db = new BlogContext()) { | |
var posts = db.Posts.ToList(); | |
} |
This file contains 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
// Entity Framework - Single | |
using (var db = new BlogContext()) { | |
var post = db.Posts.Find(1); | |
} |
This file contains 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
// 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