public static Customer GetCustomerById(NorthwindEntities northwindEntities, string customerID) { var customer = northwindEntities.Customers .FirstOrDefault(c => c.CustomerID == customerID); return customer; }
public static void ModifyCustomerCompanyName(string customerID, string newCompanyName) { using (var northwindEntities = new NorthwindEntities()) { var customer = GetCustomerById(northwindEntities, customerID); customer.CompanyName = newCompanyName; northwindEntities.SaveChanges(); } }
public static void DeleteCustomer(string customerID) { using (var northwindEntities = new NorthwindEntities()) { Customer customer = GetCustomerById(northwindEntities, customerID); northwindEntities.Customers.Remove(customer); northwindEntities.SaveChanges(); } }
public static void Main() { using (var db = new NorthwindEntities()) { var categories = db.Categories.Count(); Console.WriteLine(categories); } }
public static void Main() { using (var northwindEntities = new NorthwindEntities()) { // To solve this task you need to change in the app.config file the connection string to: // initial catalog=NorthwindTwin var result = northwindEntities.Database.CreateIfNotExists(); Console.WriteLine("Database NorthWindTwin is created: {0}", result ? "YES!" : "NO!"); } }
public static string InsertNewCustomer(Customer customer) { if (customer == null) { throw new ArgumentNullException("You can not pass null as parameter"); } using (var northwindEntities = new NorthwindEntities()) { northwindEntities.Customers.Add(customer); northwindEntities.SaveChanges(); return customer.CustomerID; } }
public static void Main() { using (var northwindEntities = new NorthwindEntities()) { var employee = northwindEntities.Employees.First(); // The new model for the Employee is in 01.CreateDbContextForNorthwind // in the file EmployeeExtended.cs EntitySet<Territory> territories = employee.TerritoriesSet; Console.WriteLine("All territories for employee {0} are:", employee.FirstName); foreach (var territory in territories) { Console.WriteLine(territory.TerritoryDescription); } } }
public static void Main() { const string nativeSqlQuery = "SELECT c.ContactName AS [Customer], o.OrderDate [Order Year], o.ShipCountry " + "FROM Customers c " + "JOIN Orders o " + "ON c.CustomerID = o.CustomerID " + "WHERE YEAR(o.OrderDate) = 1997 AND o.ShipCountry = 'Canada'"; using (var db = new NorthwindEntities()) { var customers = db.Database.SqlQuery<View>(nativeSqlQuery); foreach (var customer in customers) { Console.WriteLine(customer); } } }
public static void Main() { const string region = "SP"; var startDate = new DateTime(1995, 5, 10); var endDate = new DateTime(1996, 12, 4); using (var db = new NorthwindEntities()) { var sales = db.Orders .Where(o => o.ShipRegion == region && o.OrderDate >= startDate && o.OrderDate <= endDate) .ToList(); foreach (var sale in sales) { Console.WriteLine("{0} | {1}", sale.ShipRegion, sale.OrderDate); } } }
/// <summary> /// Problem solved by only using only one connection or introducing transactions isolation levels /// </summary> public static void Main() { var firstConection = new NorthwindEntities(); var secondConection = new NorthwindEntities(); var customerFromFirstCon = firstConection.Customers.First(); var customerFromSecondCon = secondConection.Customers.First(); Console.WriteLine("Inital Name FisrtCon: {0} - SecondCon: {1}", customerFromFirstCon.CompanyName, customerFromSecondCon.CompanyName); customerFromFirstCon.CompanyName = "Mercedes"; customerFromSecondCon.CompanyName = "Jaguar"; firstConection.SaveChanges(); secondConection.SaveChanges(); var result = new NorthwindEntities().Customers.First(); Console.WriteLine("Name After Change {0}", result.CompanyName); }
public static void Main() { using (var db = new NorthwindEntities()) { var customers = db.Customers .Join(db.Orders, (c => c.CustomerID), (o => o.CustomerID), (c, o) => new { CustomerName = c.ContactName, OrderYear = o.OrderDate.Value.Year, o.ShipCountry }) .ToList() .FindAll(c => c.OrderYear == 1997 && c.ShipCountry == "Canada") .ToList(); foreach (var cust in customers) { Console.WriteLine(cust); } } }