static void Main(string[] args) { // task - 7 - https://msdn.microsoft.com/en-us/data/jj592904.aspx for (int i = 0; i < 5; i++) { var firstConection = new NWEntities(); var secondConection = new NWEntities(); Console.WriteLine($"Now i = {i}"); // Original company name "Alfreds Futterkiste" var firstCustomer = firstConection.Customers .Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); var secondCustomer = secondConection.Customers .Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); Console.WriteLine("Name from first connection: " + firstCustomer.CompanyName); Console.WriteLine("Name from second connection: " + secondCustomer.CompanyName); firstCustomer.CompanyName = "Tlerik 1"; secondCustomer.CompanyName = "Telerik 2"; secondConection.SaveChanges(); firstConection.SaveChanges(); var result = new NWEntities() .Customers.Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); Console.WriteLine("Final company name {0}", result.CompanyName); Console.WriteLine(); } }
static void Main() { for (int i = 0; i < 5; i++) { var firstConection = new NWEntities(); var secondConection = new NWEntities(); Console.WriteLine($"Now i = {i}"); var firstCustomer = firstConection.Customers .Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); var secondCustomer = secondConection.Customers .Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); Console.WriteLine("First connection name: " + firstCustomer.CompanyName); Console.WriteLine("Second connection name: " + secondCustomer.CompanyName); firstCustomer.CompanyName = "Telerik 1"; secondCustomer.CompanyName = "Telerik 2"; secondConection.SaveChanges(); firstConection.SaveChanges(); var result = new NWEntities() .Customers.Where(x => x.ContactName == "Maria Anders") .FirstOrDefault(); Console.WriteLine("Final company name {0}", result.CompanyName); Console.WriteLine(); } }
// POST: api/Productos public void Post([FromBody] Product product) { try { using (var db = new NWEntities()) { var myProduct = db.Products.Where(x => x.ProductID == product.ProductID).FirstOrDefault(); if (myProduct != null) { db.Entry(myProduct).State = EntityState.Modified; db.Entry(myProduct).CurrentValues.SetValues(product); } else { db.Products.Add(product); } db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static void DeleteCustomer(string customerId) { var northwind = new NWEntities(); var customer = GetCustomerById(northwind, customerId); northwind.Customers.Remove(customer); northwind.SaveChanges(); }
public static void ModifyContactName(string customerId, string contactName) { var northwind = new NWEntities(); var customer = GetCustomerById(northwind, customerId); customer.ContactName = contactName; northwind.SaveChanges(); }
public static void Main() { var northwind = new NWEntities(); CustomerModifier.AddCustomer("TELER", "Telerik", "Doncho Minkov", "Trainer", "Al. Malinov 33", "Sofia", "SC", "1712", "Bulgaria", "02987654", "02345678"); CustomerModifier.ModifyCompanyName("TELER", "Telerik, Progress"); CustomerModifier.DeleteCustomer("TELER"); northwind.SaveChanges(); Console.WriteLine("Customers who shipped to Canada in 1997: "); FindCustomersByOrdersYearAndCountry(northwind, 1997, "Canada"); FindCustomersByOrdersYearAndCountryWithNativeSql(northwind, 1997, "Canada"); Console.WriteLine("Orders shipped to Sao Paulo region between 18 and 19 years ago."); FindSalesByRegionAndTimePeriod(northwind, "SP", DateTime.Now.AddYears(-19), DateTime.Now.AddYears(-18)); }
public static string AddCustomer(string customerId, string companyName, string contactName, string contactTitle, string address, string city, string region, string postalCode, string country, string phone, string fax) { var northwind = new NWEntities(); var customer = new Customer() { CustomerID = customerId, CompanyName = companyName, ContactName = contactName, ContactTitle = contactTitle, Address = address, City = city, Region = region, PostalCode = postalCode, Country = country, Phone = phone, Fax = fax, }; northwind.Customers.Add(customer); northwind.SaveChanges(); return(customer.CustomerID); }