static void Main(string[] args) { Customer newCustmer = new Customer(); newCustmer.CustomerID = "KULO"; newCustmer.CompanyName = "Mala"; newCustmer.ContactName = "Misoto Kulano"; newCustmer.ContactTitle = "Owner"; newCustmer.Address = "Amela str 23"; newCustmer.City = "Pelon"; newCustmer.PostalCode = "1231"; newCustmer.Country = "France"; newCustmer.Phone = "3443-4323-432"; newCustmer.Fax = "3245-243"; using (var otherDataBase = new NorthwindEntities()) { using (var dataBase = new NorthwindEntities()) { otherDataBase.Customers.Add(newCustmer); otherDataBase.SaveChanges(); dataBase.Customers.Attach(newCustmer); dataBase.Customers.Remove(newCustmer); dataBase.SaveChanges(); } } }
private static void AddThreeOrdersUsingTransaction() { // Note: max lenght of shipCity is 15. string[] shipCityNames = { "Persepolis", "Cartagen", "Chargoggagoggmanchauggagoggchaubunagungamaugg Lake" }; using (TransactionScope scope = new TransactionScope()) { using (NorthwindEntities context = new NorthwindEntities()) { try { foreach (var shipCity in shipCityNames) { Order order = new Order { ShipCity = shipCity }; context.Orders.Add(order); context.SaveChanges(); } scope.Complete(); } catch (DbEntityValidationException deve) { DbEntityValidationResult result = deve.EntityValidationErrors.First(); DbValidationError error = result.ValidationErrors.First(); Console.WriteLine("Invalid order property: {0}", error.PropertyName); } } } }
static void Main(string[] args) { var db = new NorthwindEntities(); var region = "WA"; var startDate = new DateTime(1996, 10, 1); var endDate = new DateTime(1998, 10, 1); FindSalesByRegionAndPeriod(region, startDate, endDate); }
static void Main(string[] args) { using (var context = new NorthwindEntities()) { var result = context.usp_FindTotalIncome(new DateTime(1994, 1, 1), new DateTime(2000, 12, 31), "Exotic Liquids").First(); Console.WriteLine("All Income are {0}", result); } }
static void Main(string[] args) { var db = new NorthwindEntities(); var empInLondon = db.Employees.Where(e => e.City == "London").Select(e => e.FirstName); foreach (var emp in empInLondon) { Console.WriteLine(emp); } }
static void Main(string[] args) { var db = new NorthwindEntities(); var query = "SELECT c.ContactName AS Name, YEAR(o.ShippedDate) AS ShippedYear, o.ShipCountry AS Destination FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID WHERE YEAR(o.ShippedDate) = 1997 AND o.ShipCountry = 'Canada'"; var selectedCustomers = db.Database.SqlQuery<CustomerAndOrder>(query); foreach (var customer in selectedCustomers) { Console.WriteLine("Name: {0} | Year: {1} | Destination: {2}", customer.Name, customer.ShippedYear, customer.Destination); } }
private static void FindSalesByRegionAndPeriod(string region, DateTime startDate, DateTime endDate) { var db = new NorthwindEntities(); var sales = db.Orders.Where(o => o.ShipRegion == region && o.OrderDate >= startDate && o.OrderDate <= endDate) .Select(o => new { Region = o.ShipRegion, OrderDate = o.OrderDate }); foreach (var sale in sales) { Console.WriteLine("Region: {0} - OrderDate {1}", sale.Region, sale.OrderDate); } }
private static void GetLastThreeCities() { using (NorthwindEntities context = new NorthwindEntities()) { var maxOrderId = context.Orders.OrderByDescending(x => x.OrderID).First().OrderID; var lastThreeOrders = (from o in context.Orders where o.OrderID > maxOrderId - 3 select new { o.ShipCity, o.OrderID }); foreach (var orderInfo in lastThreeOrders) { Console.WriteLine("City: {0} OrderId: {1}", orderInfo.ShipCity, orderInfo.OrderID); } } }
static void Main(string[] args) { var db = new NorthwindEntities(); var selectedCustomers = db.Orders .Where(o => o.ShippedDate.Value.Year == 1997 && o.ShipCountry == "Canada") .Select(o => new { Name = o.Customer.ContactName, ShippedYear = o.ShippedDate.Value.Year, Destination = o.ShipCountry }); foreach (var customer in selectedCustomers) { Console.WriteLine("Name: {0} | Year: {1} | Destination: {2}", customer.Name, customer.ShippedYear, customer.Destination); } }
static void Main(string[] args) { IObjectContextAdapter dataBase = new NorthwindEntities(); string northwindScript = dataBase.ObjectContext.CreateDatabaseScript(); string createNorthwindCloneDB = "USE master; " + "CREATE DATABASE NorthwindTwin; " + "SELECT name, size, size*1.0/128 AS [Size in MBs] " + "FROM sys.master_files " + "WHERE name = N'NorthwindTwin'; "; SqlConnection dbCon = new SqlConnection("Server=.; " + "Database=master; Integrated Security=true"); dbCon.Open(); using (dbCon) { SqlCommand createCloneDB = new SqlCommand(createNorthwindCloneDB, dbCon); createCloneDB.ExecuteNonQuery(); string changeDB = "USE NorthwindTwin; "; SqlCommand changeDataB = new SqlCommand(changeDB, dbCon); changeDataB.ExecuteNonQuery(); SqlCommand cloneDB = new SqlCommand(northwindScript, dbCon); cloneDB.ExecuteNonQuery(); } }