private static void EntityQuery() { AdventureWorksEdm context = new AdventureWorksEdm(); var people = context.People.Select(p => new { p.FirstName, p.LastName }) .Where(p => p.FirstName == "Bob") .OrderBy(p => p.LastName) .ThenBy(p => p.FirstName) .Take(5); foreach (var person in people) { Console.WriteLine("{0}, {1}", person.LastName, person.FirstName); } }
private static void EntityCrud() { using (AdventureWorksEdm context = new AdventureWorksEdm()) { var mattQuery = context.People.Where(p => p.FirstName == "Matt" && p.LastName == "Anderson"); // Show that Matt does not exist in the database. Console.WriteLine("Matt's ID is {0}", mattQuery .FirstOrDefault()? .BusinessEntityID .ToString() ?? "<null>"); var person = new Person { BusinessEntity = new BusinessEntity { rowguid = Guid.NewGuid(), ModifiedDate = DateTime.Now }, FirstName = "Matt", LastName = "Anderson", PersonType = "EM", rowguid = Guid.NewGuid(), ModifiedDate = DateTime.Now }; // Create a new person. context.People.Add(person); context.SaveChanges(); // Read the person. var matt = mattQuery.First(); Console.WriteLine("Matt's ID is {0}", matt.BusinessEntityID.ToString() ?? "<null>"); // Update the person. matt.MiddleName = "Douglas"; context.SaveChanges(); // Read the new person. Console.WriteLine("Matt's middle name is {0}", matt.MiddleName ?? "<null>"); // Delete the person. context.People.Remove(matt); context.SaveChanges(); // Show that Matt does not exist in the database. Console.WriteLine("Matt's ID is {0}", mattQuery.FirstOrDefault()?.BusinessEntityID.ToString() ?? "<null>"); } }