static void Main(string[] args)
        {
            Customer newCustmer = new Customer();
            newCustmer.CustomerID = "KULO";
            newCustmer.CompanyName = "Mala";
            newCustmer.ContactName = "Misoto Kulano";
            newCustmer.ContactTitle = "Owner";
            newCustmer.Address = "Amela str 23";
            newCustmer.City = "Pelon";
            newCustmer.PostalCode = "1231";
            newCustmer.Country = "France";
            newCustmer.Phone = "3443-4323-432";
            newCustmer.Fax = "3245-243";

            using (var otherDataBase = new NorthwindEntities())
            {

                using (var dataBase = new NorthwindEntities())
                {
                    otherDataBase.Customers.Add(newCustmer);
                    otherDataBase.SaveChanges();
                    dataBase.Customers.Attach(newCustmer);
                    dataBase.Customers.Remove(newCustmer);
                    dataBase.SaveChanges();
                }
            }
        }
Beispiel #2
0
        private static void AddThreeOrdersUsingTransaction()
        {
            // Note: max lenght of shipCity is 15.
            string[] shipCityNames = { "Persepolis", "Cartagen", "Chargoggagoggmanchauggagoggchaubunagungamaugg Lake" };

            using (TransactionScope scope = new TransactionScope())
            {
                using (NorthwindEntities context = new NorthwindEntities())
                {
                    try
                    {
                        foreach (var shipCity in shipCityNames)
                        {
                            Order order = new Order
                            {
                                ShipCity = shipCity
                            };

                            context.Orders.Add(order);
                            context.SaveChanges();
                        }

                        scope.Complete();
                    }
                    catch (DbEntityValidationException deve)
                    {
                        DbEntityValidationResult result = deve.EntityValidationErrors.First();
                        DbValidationError error = result.ValidationErrors.First();

                        Console.WriteLine("Invalid order property: {0}",
                            error.PropertyName);
                    }
                }
            }
        }