private static void Main() { using (var northwindEntities = new NorthwindEntities()) { ExecuteMethodInsideTransaction(northwindEntities, PlaceOrder); } }
private static void ExecuteCommandSafely(Action<NorthwindEntities> command) { using (var databaseContext = new NorthwindEntities()) { command(databaseContext); databaseContext.SaveChanges(); } }
private static decimal FindSupplierIncomesForPeriodOfTime(string name, DateTime startDate, DateTime endDate) { using (var northwindEntities = new NorthwindEntities()) { var outputParameter = new ObjectParameter("result", typeof(decimal)); northwindEntities.FindSupplierIncome(name, startDate, endDate, outputParameter); return decimal.Parse(outputParameter.Value.ToString()); } }
private static void Main() { using (var northwindConnection = new NorthwindEntities()) { var extendedEmployee = new ExtendedEmployee(); Console.WriteLine(extendedEmployee.Territory); } }
private static void PlaceOrder(NorthwindEntities northwindEntities) { var order = new Order { CustomerID = "QUICK", OrderDate = DateTime.Now, ShipAddress = "At home :)", ShipCity = "My City" }; for (var i = 1; i < 5; i++) { order.Order_Details.Add( new Order_Detail { Discount = (float)i / 10, ProductID = i, Quantity = (short)i, UnitPrice = i }); } northwindEntities.Orders.Add(order); northwindEntities.SaveChanges(); }
private static void ExecuteMethodInsideTransaction( NorthwindEntities northwindEntities, Action<NorthwindEntities> transactionActions) { using (var orderTransaction = northwindEntities.Database.BeginTransaction()) { try { transactionActions(northwindEntities); orderTransaction.Commit(); Console.WriteLine("The transaction succeeded"); } catch (Exception exception) { orderTransaction.Rollback(); Console.WriteLine("The transaction failed and it was rolled back"); Console.WriteLine(exception.Message); } } }
private static void PrintCusomersUsingEntityFramework() { using (var northWindDb = new NorthwindEntities()) { var custumers = from clients in northWindDb.Customers where clients.Orders.Any( order => order.OrderDate != null && order.OrderDate.Value.Year == ReportYear && order.ShipCountry == ReportCountry) select clients.ContactName; Console.WriteLine("Using entity framework ..."); foreach (var custumer in custumers) { Console.WriteLine(custumer); } } }
private static void PrintCustomersUsingNativeQuery() { using (var northWindDb = new NorthwindEntities()) { const string NativeSqlQuery = "SELECT DISTINCT cust.ContactName FROM Customers cust " + "JOIN Orders ord " + "ON ord.CustomerID = cust.CustomerID " + "WHERE ord.CustomerID IS NOT NULL AND " + " ({0} = (DATEPART (year, ord.OrderDate))) AND (N'{1}' = ord.ShipCountry)"; object[] parameters = { ReportYear, ReportCountry }; Console.WriteLine("Using Native query ..."); var customers = northWindDb.Database.SqlQuery<string>(string.Format(NativeSqlQuery, parameters)); foreach (var custumer in customers) { Console.WriteLine(custumer); } } }
private static void Main() { var firstConnection = new NorthwindEntities(); var secondConnection = new NorthwindEntities(); for (var index = 0; index < 5; index++) { firstConnection.Categories.Add(new Category { CategoryName = "firstConn" + index }); secondConnection.Categories.Add(new Category { CategoryName = "secondConn" + index }); firstConnection.SaveChanges(); secondConnection.SaveChanges(); } var firstEntry = firstConnection.Categories.First(x => x.CategoryName == "firstConn0"); firstEntry.CategoryName = "changed"; var sameEntry = secondConnection.Categories.First(x => x.CategoryName == "firstConn0"); secondConnection.Categories.Remove(sameEntry); firstConnection.SaveChanges(); }
private static void GetSales(string region, DateTime startDateTime, DateTime endDateTime) { using (var northwind = new NorthwindEntities()) { var sales = from orders in northwind.Orders join orderDetails in northwind.Order_Details on orders.OrderID equals orderDetails.OrderID where orders.ShipRegion == region && orders.OrderDate > startDateTime && orders.OrderDate < endDateTime select orderDetails; foreach (var sale in sales) { Console.WriteLine( "OrderID: {0}, ProductID: {1}, Sale Quantity: {2}, Sale Price {3}", sale.OrderID, sale.ProductID, sale.Quantity, sale.UnitPrice); } } }
private static void CreateProcedureIfNeeded() { const string CreateProcedure = @"USE [Northwind] IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('[dbo].[FindSupplierIncome]')) BEGIN exec('CREATE PROCEDURE [dbo].[FindSupplierIncome](@name varchar(100), @startDate Date, @endDate Date, @result money OUTPUT) AS BEGIN SELECT @result = SUM(det.UnitPrice * det.Quantity) FROM [Order Details] det JOIN Products prd ON prd.ProductID = det.ProductID JOIN Suppliers spl ON spl.SupplierID = prd.SupplierID JOIN Orders ord ON ord.OrderID = det.OrderID WHERE spl.CompanyName = @name AND ord.OrderDate > @startDate AND ord.OrderDate < @endDate END ') END"; using (var northwindEntities = new NorthwindEntities()) { northwindEntities.Database.ExecuteSqlCommand(CreateProcedure); } }
private static bool CustomerExists(NorthwindEntities databaseContext, string custumerId) { return databaseContext.Customers.Any(cust => cust.CustomerID == custumerId); }