Ejemplo n.º 1
0
        public static void Slow()
        {
            var db = new globalfactory2021Context();

            var productName = "DummyP";

            for (int i = 0; i < 1000; i++)
            {
                var product = new Product
                {
                    Name           = productName,
                    Price          = 11,
                    Manufacturerid = 2
                };
                db.Products.Add(product);
            }

            var toRemoves = db.Products.Where(p => p.Name == productName).ToList();

            foreach (var toRemove in toRemoves)
            {
                db.Products.Remove(toRemove);
            }

            db.Dispose();
        }
Ejemplo n.º 2
0
        public static void Fast()
        {
            var db = new globalfactory2021Context();

            var productName = "DummyP";
            var toAdd       = new List <Product>();

            for (int i = 0; i < 1000; i++)
            {
                var product = new Product
                {
                    Name           = productName,
                    Price          = 11,
                    Manufacturerid = 2
                };
                toAdd.Add(product);
            }
            db.Products.AddRange(toAdd);

            var toRemoves = db.Products.Where(p => p.Name == productName).ToList();

            db.Products.RemoveRange(toRemoves);

            db.Dispose();
        }
Ejemplo n.º 3
0
        private static void SeedData(globalfactory2021Context db)
        {
            var manufacturer = new Manufacturer
            {
                Country = "Test country",
                Name    = ManufacturerName
            };

            var products = new List <Product>();
            var random   = new Random();

            for (var i = 0; i < 10000; i++)
            {
                var product = new Product
                {
                    Name         = (i % 2 == 0) ? TestProduct1Name : TestProduct2NameNotPadded.PadRight(13),
                    Manufacturer = manufacturer,
                    Price        = (decimal)random.NextDouble() * MaxPrice
                };

                products.Add(product);
            }

            manufacturer.Products = products;

            db.Manufacturers.Add(manufacturer);
            db.SaveChanges();
            db.Dispose();
        }
Ejemplo n.º 4
0
        public static void Default()
        {
            var db = new globalfactory2021Context();

            var product = db.Products
                          .ToList();

            db.Dispose();
        }
Ejemplo n.º 5
0
            /// <summary>
            /// Key difference: equals
            /// </summary>
            public static void Slow()
            {
                var db = new globalfactory2021Context();

                var filtered = db.Products
                               .Where(p => p.Name.Equals(DataSeeding.TestProduct1Name))
                               .ToList();

                db.Dispose();
            }
Ejemplo n.º 6
0
        public static void Lazy()
        {
            var db = new globalfactory2021Context();

            var product = db.Products.First();
            // Lazy loaded
            var manufacturer = product.Manufacturer;

            db.Dispose();
        }
        /// <summary>
        /// Key difference: format multiple times
        /// </summary>
        public static void Slow()
        {
            var db = new globalfactory2021Context();

            var filtered = db.Products
                           .Where(p => p.Name.Trim() == DataSeeding.TestProduct2NameNotPadded)
                           .ToList();

            db.Dispose();
        }
Ejemplo n.º 8
0
        public static void AsNoTracking()
        {
            var db = new globalfactory2021Context();

            var product = db.Products
                          .AsNoTracking()
                          .ToList();

            db.Dispose();
        }
Ejemplo n.º 9
0
            /// <summary>
            /// Key difference: IEnumerable
            /// </summary>
            public static void Slow()
            {
                var db = new globalfactory2021Context();

                IEnumerable <Product> products = db.Products;
                var filtered = products
                               .Where(p => p.Name == DataSeeding.TestProduct1Name)
                               .ToList();

                db.Dispose();
            }
Ejemplo n.º 10
0
            /// <summary>
            /// Key difference: ==
            /// </summary>
            public static void Fast()
            {
                var db = new globalfactory2021Context();

                // Some expressions were meant for direct C#-To-SQL translation.
                // Others, like .equals, were not.
                var filtered = db.Products
                               .Where(p => p.Name == DataSeeding.TestProduct1Name)
                               .ToList();

                db.Dispose();
            }
Ejemplo n.º 11
0
        public static void Eager()
        {
            var db = new globalfactory2021Context();

            var manufacturer = db.Products
                               // Eager loaded
                               .Include(p => p.Manufacturer)
                               .First()
                               .Manufacturer;

            db.Dispose();
        }
Ejemplo n.º 12
0
            /// <summary>
            /// Key difference: IQueryable
            /// </summary>
            public static void Fast()
            {
                var db = new globalfactory2021Context();

                // IQueryable is a data structure still able to translate a C# expression to a SQL query.
                // SQL is much faster for lookups than C#.
                // 10 times faster.
                IQueryable <Product> products = db.Products;
                var filtered = products
                               .Where(p => p.Name == DataSeeding.TestProduct1Name)
                               .ToList();

                db.Dispose();
            }
        /// <summary>
        /// Key difference: format once
        /// </summary>
        public static void Fast()
        {
            var db = new globalfactory2021Context();

            // When using char or nchar- you will have a padded field
            // In order to find it, you will need to to make both sides:
            // filter and the column- to be of the same format
            // instead of trimming every column (n)
            // format the filter to match the formatting of a column (1)
            var filter   = DataSeeding.TestProduct2NameNotPadded.PadLeft(13);
            var filtered = db.Products
                           .Where(p => p.Name == filter)
                           .ToList();

            db.Dispose();
        }