Created
February 21, 2013 11:24
-
-
Save yetanotherchris/5004068 to your computer and use it in GitHub Desktop.
ORMS: linq to sql
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
// The Person class is a lot bigger than this after the Entity Framework Designer has got its grubby paws on it | |
// I've included this shorter version just so you can see what a Person class is | |
public class Person | |
{ | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public string Email { get; set; } | |
} | |
public class PersonManager | |
{ | |
public static void Example() | |
{ | |
// Requires app.config/web.config connection string settings | |
// This 'EntityManager' is created for you by the designer | |
using (EntityManager manager = new EntityManager()) | |
{ | |
// Save example | |
Person person = new Person(); | |
person.Id = Guid.NewGuid(); | |
person.Name = "John"; | |
person.Email = "john@aol.com"; | |
manager.SaveChanges(person); | |
// Getting everything | |
IList<Person> list = manager.PersonItemSet.ToList(); | |
// Paging example, use Include() to include parent/child objects in the query. | |
// The include statement indicts to EF to join on the tables, and not lazy load. | |
int page = 1; | |
int itemsPerPage = 10; | |
var x = entities.PersonItemSet | |
.Include("Address") | |
.Include("ParentCompany.Address") | |
.Where<Person>(p => p.Name == "John") | |
.OrderBy(i => i.Name) | |
.Skip(page) | |
.Take(itemsPerPage); | |
// The benefit of the EF is the above linq produces a joined query any dba would be proud of | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment