public static void AddOrderInTransactionScope()
        {
            Console.WriteLine("Adding two orders using TransactionScope...");
            using (NorthwindEntities northwindEntities = new NorthwindEntities())
            {
                Order firstOrder = new Order();
                firstOrder.CustomerID = "TOMSP";
                firstOrder.ShipName = "First Order Ship Name";
                Order secondOrder = new Order();
                secondOrder.CustomerID = "OCEAN";
                secondOrder.ShipName = "Second Order Ship Name";

                using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
                {
                    northwindEntities.Orders.Add(firstOrder);
                    northwindEntities.SaveChanges();

                    northwindEntities.Orders.Add(secondOrder);
                    northwindEntities.SaveChanges();

                    scope.Complete();
                }

                firstOrder = northwindEntities.Orders.Find(firstOrder.OrderID);
                secondOrder = northwindEntities.Orders.Find(secondOrder.OrderID);
                Console.WriteLine("FirstOrder.ShipName={0}, SecondOrder.ShipName={1}", firstOrder.ShipName, secondOrder.ShipName);
            }
        }
        public static bool Delete(string customerID)
        {
            using (var ctx = new NorthwindEntities())
            {
                var theCustomer = ctx.Customers.Find(customerID);

                if (theCustomer == null) return false;

                ctx.Customers.Remove(theCustomer);
                if (ctx.SaveChanges() == 0) return false;
            }

            return true;
        }
        public static Customer Insert(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 newCustomer = new Customer();
            newCustomer.CompanyName = companyName;
            newCustomer.ContactName = contactName;
            newCustomer.ContactTitle = contactTitle;
            newCustomer.Address = address;
            newCustomer.City = city;
            newCustomer.Region = region;
            newCustomer.PostalCode = postalCode;
            newCustomer.Country = country;
            newCustomer.Phone = phone;
            newCustomer.Fax = fax;

            string[] getWords = companyName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            newCustomer.CustomerID = getWords[0].Substring(0, 3) +
                ((getWords.Length > 1) ? getWords[1].Substring(0, 2) : getWords[0].Substring(0, 2));
            newCustomer.CustomerID = newCustomer.CustomerID.ToUpperInvariant();

            using (var ctx = new NorthwindEntities())
            {
                while (ctx.Customers.Find(newCustomer.CustomerID) != null && newCustomer.CustomerID != LastPossibleCustomerId)
                {
                    for (int i = newCustomer.CustomerID.Length-1; i >= 0 ; i--)
                    {
                        if (newCustomer.CustomerID[i] < 'Z')
                        {
                            char[] letters = newCustomer.CustomerID.ToCharArray();
                            letters[i]++;
                            newCustomer.CustomerID = string.Join("", letters);
                            break;
                        }
                    }
                }
                ctx.Customers.Add(newCustomer);
                ctx.SaveChanges();
            }

            return newCustomer;
        }
        public static void ConcurentChanges()
        {
            using (var ctx1 = new NorthwindEntities())
            {
                Employee employeeFromCtx1 = ctx1.Employees.First(e => e.EmployeeID == 1);
                employeeFromCtx1.FirstName = "Pesho";

                // Modify and Save the same employee in another context
                // i.e. mimicking concurrent access.
                using (var ctx2 = new NorthwindEntities())
                {
                    Employee employeeFromCtx2 = ctx2.Employees.First(e => e.EmployeeID == 1);
                    employeeFromCtx2.FirstName = "Gosho";
                    ctx2.SaveChanges();
                }
                // Console.ReadLine();
                // Save the changes... This should result in an Exception, but NOT due to EF 5 Optimistic Concurrency
                ctx1.SaveChanges();
            }
        }
        public static Customer Modify(string customerID,
            string companyName = null,
            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)
        {
            using (var ctx = new NorthwindEntities())
            {
                var theCustomer = ctx.Customers.Find(customerID);

                if (theCustomer == null) return null;
                if (companyName != null) theCustomer.CompanyName = companyName;
                if (contactName != null) theCustomer.ContactName = contactName;
                if (contactTitle != null) theCustomer.ContactTitle = contactTitle;
                if (address != null) theCustomer.Address = address;
                if (city != null) theCustomer.City = city;
                if (region != null) theCustomer.Region = region;
                if (postalCode != null) theCustomer.PostalCode = postalCode;
                if (country != null) theCustomer.Country = country;
                if (phone != null) theCustomer.Phone = phone;
                if (fax != null) theCustomer.Fax = fax;
                ctx.SaveChanges();
                return theCustomer;
            }
        }
        public static void CreateUserInGroup()
        {
            Console.WriteLine("Adding new user 'mancho' and attach it to the 'Admins' group...");
            string username = "******";

            using (var northwindEntities = new NorthwindEntities())
            {
                using (var scope = new System.Transactions.TransactionScope())
                {
                    User someUser = new User();
                    someUser.Username = username;
                    someUser.FullName = "Chicho Mancho";
                    someUser.Password = "******";

                    string adminGroup = "Admins";
                    var group = northwindEntities.Groups.Where(a => a.Name == adminGroup).ToList();
                    if (group.Count == 0)
                    {
                        Group newGroup = new Group();
                        newGroup.Name = adminGroup;

                        northwindEntities.Groups.Add(newGroup);
                        northwindEntities.SaveChanges();
                        group.Add(newGroup);
                    }

                    someUser.GroupId = group[0].GroupId;
                    northwindEntities.Users.Add(someUser);
                    northwindEntities.SaveChanges();
                    scope.Complete();
                }
            }
        }