Пример #1
0
        public void NorthwindContainerTest()
        {
            var contexto = new NorthwindContainer();

            var categoria = new Categoria();

            categoria.Nome = "Papelaria";

            contexto.Categoria.Add(categoria);

            contexto.SaveChanges();
        }
Пример #2
0
        private static void LinqToEntities()
        {
            var custQuery           = "SELECT c FROM NorthwindContainer.CustomerSet AS c WHERE c.Country = 'Italy'";
            var custValQuery        = "SELECT VALUE c FROM NorthwindContainer.CustomerSet AS c WHERE c.Country = 'Italy'";
            var northwindConnection = ConfigurationManager.ConnectionStrings["NorthwindContainer"].ConnectionString;

            Console.WriteLine("LINQ To Entities ----------------------------------------------------------");

            using (var conn = new EntityConnection(northwindConnection))
                using (var cmd = new EntityCommand(custQuery, conn))
                {
                    conn.Open();

                    using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (reader.Read())
                        {
                            var c = (DbDataRecord)reader.GetValue(0);
                            Console.WriteLine(string.Format("{0} - {1} - {2}", c[2], c[6], c[8]));
                        }
                    }

                    conn.Close();
                }

            using (var container = new NorthwindContainer())
            {
                var query = container.CreateQuery <NorthwindCustomer>(custValQuery);

                foreach (var item in query)
                {
                    Console.WriteLine("{0} - {1} - {2}", item.CustomerID, item.CompanyName, item.Phone);
                }

                var query2 =
                    from c in container.CustomerSet
                    where c.Country == "Italy"
                    select new { c.CustomerID, c.CompanyName, c.Address };

                foreach (var item in query2)
                {
                    Console.WriteLine(item);
                }

                var query3 =
                    (from o in container.OrderSet
                     where o.ShipCountry == "USA"
                     select o.Customer).Distinct();

                foreach (var item in query3)
                {
                    Console.WriteLine("{0} - {1} - {2}", item.CustomerID, item.CompanyName, item.Region);
                }
            }

            using (var container = new NorthwindContainer())
            {
                var customer =
                    (from c in container.CustomerSet
                     where c.CustomerID == "ALFKI"
                     select c).First();

                var customer2 =
                    (from c in container.CustomerSet
                     where c.CustomerID == "ALFKI"
                     select c).First();

                var originalContact = customer.ContactName;

                Console.WriteLine("{0} - {1}", customer.CustomerID, customer.GetHashCode());
                Console.WriteLine("{0} - {1}", customer2.CustomerID, customer2.GetHashCode());
                Console.WriteLine("{0} - {1}", customer2.CustomerID, customer.Equals(customer2));

                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);
                customer.ContactName = "Michael Petersen - Changed";
                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);
                container.SaveChanges();
                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);

                customer.ContactName = originalContact;
                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);
                container.SaveChanges(false);
                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);
                container.AcceptAllChanges();
                Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);
            }

            using (var scope = new TransactionScope())
                using (var container = new NorthwindContainer())
                {
                    Transaction.Current.TransactionCompleted +=
                        (s, e) => { Console.WriteLine(e.Transaction.TransactionInformation.Status); };

                    var customer =
                        (from c in container.CustomerSet
                         where c.CustomerID == "ALFKI"
                         select c).First();

                    var originalContact = customer.ContactName;
                    customer.ContactName = "Michael Petersen - Changed";
                    customer.ContactName = originalContact;
                    Console.WriteLine("{0} - {1} - {2}", customer.CustomerID, customer.ContactName, customer.EntityState);

                    container.SaveChanges();
                    scope.Complete();
                }

            Console.WriteLine();
        }