public static void GenerateSqlUsingDbMigrator()
 {
     using (var context = new AdventureWorksContext())
     {
         var configuration = new Configuration
             {
                 ContextType = typeof(AdventureWorksContext),
                 TargetDatabase =
                     new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient")
             };
         var migrator = new DbMigrator(configuration);
         var migrations = migrator.GetDatabaseMigrations();
         if (migrations.Any())
         {
             var scriptor = new MigratorScriptingDecorator(migrator);
             string script = scriptor.ScriptUpdate(null, migrations.Last());
             if (!String.IsNullOrEmpty(script))
             {
                 Console.WriteLine(script);
                 //context.Database.ExecuteSqlCommand(script);
             }
         }                
         
         Console.ReadKey();
     }
 }
Beispiel #2
0
        public static void DemonstrateCaching()
        {
            using (var context1 = new AdventureWorksContext())
            {
                Product product = context1.Products.Single(p => p.ProductID == 1);
                product.Name = product.Name += " - Test";
                context1.SaveChanges();

                Product product2 = context1.Products.Find(1); // Will not hit the database
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
 public static void GeneratePerformanceOutput(
     Func<AdventureWorksContext, string, List<Contact>> dbCall, string outputMessage, string sql)
 {
     using (new MeasureUtil("Entity Framework Serialisation - " + outputMessage))
     using (var context = new AdventureWorksContext())
     {
         List<Contact> contactsList = dbCall(context, sql).ToList();
         DisplayContactListDetails(contactsList);
     }
 }
 public static List<Contact> TestObjectContextExecuteStoreQuery(AdventureWorksContext context, string sql)
 {
     ObjectResult<Contact> contacts =
         ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<Contact>(sql);
     return contacts.ToList();
 }
 public static List<Contact> TestDbSetSqlQueryWithTransaction(AdventureWorksContext context, string sql)
 {
     List<Contact> contacts = context.Contacts.SqlQuery(sql).ToListReadUncommitted();
     return contacts.ToList();
 }
 public static List<Contact> TestDbSetSqlQueryAsNoTracking(AdventureWorksContext context, string sql)
 {
     DbSqlQuery<Contact> contacts = context.Contacts.SqlQuery(sql).AsNoTracking();
     return contacts.ToList();
 }
 public static List<Contact> TestDatabaseSqlQuery(AdventureWorksContext context, string sql)
 {
     DbRawSqlQuery<Contact> contacts = context.Database.SqlQuery<Contact>(sql);
     return contacts.ToList();
 }
        public static void ShowSelect()
        {
            //Lazy load entity
            using (var context = new AdventureWorksContext())
            {
                var products = context.Products
                    .Where(p => p.ProductSubcategory != null).Take(10)
                    .Select(p => p);

                foreach (var p in products)
                {
                    Console.WriteLine(p.ProductID);
                    if (p.ProductSubcategory != null)
                    {
                        Console.WriteLine(p.ProductSubcategory);
                        Console.WriteLine(p.ProductSubcategory.ProductCategoryID); // Causes an additional database hit
                    }
                    Console.WriteLine("----------------------------------------------");
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();


            //Deep load entity
            using (var context = new AdventureWorksContext())
            {
                var products = context.Products
                    .Include("ProductSubcategory")
                    .Where(p => p.ProductSubcategory != null).Take(10)
                    .Select(s => s);

                foreach (var e in products)
                {
                    Console.WriteLine(e.ProductID);
                    Console.WriteLine(e.ProductSubcategory.ProductCategoryID); // Does not cause an additional database hit
                    Console.WriteLine("----------------------------------------------");
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();


            // ------------------------ Anonymous type EF queries ----------------------------------// 

            //Select with projection using anonymous type
            using (var context = new AdventureWorksContext())
            {
                var products = context.Products.Where(p => p.ProductSubcategory != null).Take(10)
                    .Select(s => new { s.ProductID, s.Name });

                foreach (var e in products)
                {
                    Console.WriteLine(e.ProductID);
                    Console.WriteLine(e.Name); // Does not cause an additional database hit
                    Console.WriteLine("----------------------------------------------");
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();


            //View paging behaviour
            using (var context = new AdventureWorksContext())
            {
                var products = context.Products.OrderBy(o => o.ProductID)
                    .ToPagedList(50, 10); ;

                foreach (var p in products)
                {
                    Console.WriteLine(p.ProductID);
                    Console.WriteLine(p.ProductSubcategory);
                    Console.WriteLine(p.ProductSubcategory.ProductCategoryID); // Causes an additional database hit
                    Console.WriteLine("----------------------------------------------");
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }