예제 #1
0
        /// <summary>
        /// Problem solved by only using 1 db connection
        /// </summary>
        public static void Main()
        {
            using (var db1 = new NorthwindEntities())
            {
                using (var db2 = new NorthwindEntities())
                {
                    Console.WriteLine("First connection");
                    Console.WriteLine("Original -> " + db1.Employees.First().City);
                    db1.Employees.First().City = "Orlandovci";
                    Console.WriteLine("Changed -> " + db1.Employees.First().City);

                    Console.WriteLine("Second connection");
                    Console.WriteLine("Original -> " + db2.Employees.First().City);
                    db2.Employees.First().City = "Krivina";
                    Console.WriteLine("Changed -> " + db2.Employees.First().City);

                    db1.SaveChanges();

                    db2.SaveChanges();
                }
            }

            using (var db3 = new NorthwindEntities())
            {
                Console.WriteLine("Third connection");
                Console.WriteLine(db3.Employees.First().City);
            }
        }
예제 #2
0
        public static void AddCustomer(
            string customerId,
            string companyName,
            string contactName = null,
            string contactTitle = null,
            string address = null,
            string city = null,
            string region = null,
            string postalCode = null,
            string country = null,
            string phone = null,
            string fax = null)
        {
            var db = new NorthwindEntities();

            using (db)
            {
                db.Customers.Add(new Customer
                {
                    CustomerID = customerId,
                    CompanyName = companyName,
                    ContactName = contactName,
                    ContactTitle = contactTitle,
                    Address = address,
                    City = city,
                    Region = region,
                    PostalCode = postalCode,
                    Country = country,
                    Phone = phone,
                    Fax = fax
                });

                db.SaveChanges();
            }
        }
예제 #3
0
        public static void RemoveCustomer(string customerId)
        {
            var db = new NorthwindEntities();

            using (db)
            {
                var customerToRemove = db.Customers.FirstOrDefault(c => c.CustomerID == customerId);

                db.Customers.Remove(customerToRemove);

                db.SaveChanges();
            }
        }
예제 #4
0
        public static void ModifyCustomer(
            string customerId,
            string companyName,
            string contactName,
            string contactTitle,
            string address,
            string city,
            string region,
            string postalCode,
            string country,
            string phone,
            string fax)
        {
            var db = new NorthwindEntities();

            using (db)
            {
                Customer customerToModify = db.Customers.FirstOrDefault(c => c.CustomerID == customerId);

                customerToModify.CompanyName = companyName ?? customerToModify.CompanyName;
                customerToModify.ContactName = contactName ?? customerToModify.ContactName;
                customerToModify.ContactTitle = contactTitle ?? customerToModify.ContactTitle;
                customerToModify.Address = address ?? customerToModify.Address;
                customerToModify.City = city ?? customerToModify.City;
                customerToModify.Region = region ?? customerToModify.Region;
                customerToModify.PostalCode = postalCode ?? customerToModify.PostalCode;
                customerToModify.Country = country ?? customerToModify.Country;
                customerToModify.Phone = phone ?? customerToModify.Phone;
                customerToModify.Fax = fax ?? customerToModify.Fax;

                db.SaveChanges();
            }
        }