Exemplo n.º 1
0
        private static void ShowLinqToEntities()
        {
            var ef4TestEntities = new EF4TestEntities();

            var customerNames = from cn in ef4TestEntities.CustomerNames
                                select cn;

            var shortNames = from cn in ef4TestEntities.CustomerNames
                             select new { First = cn.FirstName, Last = cn.LastName };

            var spatialPoints = from sp in ef4TestEntities.SpatialPoints
                                select new { Id = sp.Id, X = sp.X, Y = sp.Y };

            // Display all initially existing data records
            foreach (var cn in customerNames)
            {
                Console.WriteLine("Customer: {0} {1} {2} {3}", cn.CustomerId, cn.FirstName, cn.LastName, cn.EmailAddress);
            }

            foreach (var sn in shortNames)
            {
                Console.WriteLine("Customer (short): {0} {1}", sn.First, sn.Last);
            }

            foreach (var sp in spatialPoints)
            {
                Console.WriteLine("Point: {0} {1} {2}", sp.Id, sp.X, sp.Y);
            }

            // Create new baseline for data within the database
            foreach (var cn in customerNames)
            {
                ef4TestEntities.CustomerNames.DeleteObject(cn);
            }

            ef4TestEntities.CustomerNames.AddObject(new CustomerName {
                FirstName = "Petra", LastName = "Jones", EmailAddress = "*****@*****.**"
            });
            ef4TestEntities.CustomerNames.AddObject(new CustomerName {
                FirstName = "Michael", LastName = "Smith", EmailAddress = "*****@*****.**"
            });
            ef4TestEntities.CustomerNames.AddObject(new CustomerName {
                FirstName = "fn1", LastName = "ln1", EmailAddress = "em1"
            });
            ef4TestEntities.SaveChanges();

            // Display newly created baseline of data
            foreach (var cn in customerNames)
            {
                Console.WriteLine("Customer: {0} {1} {2} {3}", cn.CustomerId, cn.FirstName, cn.LastName, cn.EmailAddress);
            }

            // Update existing data record
            var toUpdate = (from c in ef4TestEntities.CustomerNames
                            where c.FirstName == "fn1" && c.LastName == "ln1" && c.EmailAddress == "em1"
                            select c).First();

            toUpdate.EmailAddress = "em2";
            ef4TestEntities.SaveChanges();

            // Display updated data record
            foreach (var cn in customerNames)
            {
                Console.WriteLine("Customer: {0} {1} {2} {3}", cn.CustomerId, cn.FirstName, cn.LastName, cn.EmailAddress);
            }

            // Delete specific data record
            var toDelete = (from c in ef4TestEntities.CustomerNames
                            where c.FirstName == "fn1" && c.LastName == "ln1" && c.EmailAddress == "em2"
                            select c).First();

            ef4TestEntities.CustomerNames.DeleteObject(toDelete);
            ef4TestEntities.SaveChanges();

            // Proof that data record has beed deleted
            foreach (var cn in customerNames)
            {
                Console.WriteLine("Customer: {0} {1} {2} {3}", cn.CustomerId, cn.FirstName, cn.LastName, cn.EmailAddress);
            }
        }
Exemplo n.º 2
0
        private static void ShowEntityFramework()
        {
            var allCustomerNames          = "SELECT c FROM EF4TestEntities.CustomerNames AS c";
            var allValueCustomerNames     = "SELECT VALUE c FROM EF4TestEntities.CustomerNames AS c";
            var michaelValueCustomerNames = "SELECT VALUE c FROM EF4TestEntities.CustomerNames AS c WHERE c.FirstName = @firstName";
            var ef4TestEntities           = ConfigurationManager.ConnectionStrings["EF4TestEntities"].ConnectionString;

            // All customers as raw DbDataRecords through EntityClient provider
            using (var cn = new EntityConnection(ef4TestEntities))
                using (var cmd = new EntityCommand(allCustomerNames, cn))
                {
                    cn.Open();

                    using (var dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (dr.Read())
                        {
                            Console.WriteLine(((DbDataRecord)dr.GetValue(0)).GetValue(1));
                        }
                    }
                }

            // All customers as CustomerName value through EntityClient provider
            using (var cn = new EntityConnection(ef4TestEntities))
                using (var cmd = new EntityCommand(allValueCustomerNames, cn))
                {
                    cn.Open();

                    using (var dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (dr.Read())
                        {
                            Console.WriteLine("{0} {1}", dr.GetValue(0), dr.GetValue(1));
                        }
                    }
                }

            // All customers as raw DbDataRecords through ObjectContext
            using (var context = new EF4TestEntities())
            {
                var customerNames = context.CreateQuery <DbDataRecord>(allCustomerNames);

                foreach (var c in customerNames)
                {
                    Console.WriteLine(((CustomerName)c.GetValue(0)).FirstName);
                }
            }

            // All customers as CustomerName through ObjectContext
            using (var context = new EF4TestEntities())
            {
                var customerNames = context.CreateQuery <CustomerName>(allValueCustomerNames);

                foreach (var c in customerNames)
                {
                    Console.WriteLine("{0} {1}", c.FirstName, c.LastName);
                }
            }

            // Michael as CustomerName through ObjectContext
            using (var context = new EF4TestEntities())
            {
                var customerNames =
                    context.CreateQuery <CustomerName>(michaelValueCustomerNames,
                                                       new[] { new ObjectParameter("firstName", "Michael") });

                foreach (var c in customerNames)
                {
                    Console.WriteLine("{0} {1}", c.CustomerId, c.FirstName);
                }
            }

            // All customers as CustomerName through LINQ
            var all = from cn in new EF4TestEntities().CustomerNames
                      orderby cn.EmailAddress
                      select new { Id = cn.CustomerId, First = cn.FirstName, Last = cn.LastName };

            foreach (var cn in all)
            {
                Console.WriteLine("{0} {1}", cn.Id, cn.First, cn.Last);
            }

            // Michael as CustomerName through LINQ
            var michael = from cn in new EF4TestEntities().CustomerNames
                          where cn.FirstName == "Michael"
                          select cn;

            Console.WriteLine((michael as ObjectQuery).ToTraceString());

            foreach (var cn in michael)
            {
                Console.WriteLine("{0} {1}", cn.FirstName, cn.LastName);
            }
        }