Ejemplo n.º 1
0
        public bool UpdateProduct(ProductModel model)
        {
            using (var ctx = new AdventureWorksLT2008R2Entities())
            {
                // find old record first
                var record = ctx.Products.Find(model.ProductID);
                if (record == null)
                {
                    return(false);
                }

                var category = ctx.ProductCategories.Find(model.ProductCategoryID);

                record.ProductNumber   = model.ProductNumber;
                record.StandardCost    = model.StandardCost;
                record.Name            = model.Name;
                record.Color           = model.Color;
                record.ProductCategory = category;

                ctx.Products.Attach(record);
                ctx.Entry(record).State = EntityState.Modified;
                ctx.SaveChanges();

                return(true);
            }
        }
Ejemplo n.º 2
0
        public void TestExecuteCommand()
        {
            Customer     mrAndrewCencini;
            const string originalEmail = "*****@*****.**";
            const string anotherEmail  = "*****@*****.**";
            string       replacementEmail;

            using (var dbc = new AdventureWorksLT2008R2Entities())
            {
                dbc.Configuration.LazyLoadingEnabled = false;

                mrAndrewCencini = dbc.GetDbProxy(TestAspects).Single(db => db.QueryCustomerByID(163));

                replacementEmail             = mrAndrewCencini.EmailAddress == originalEmail ? anotherEmail : originalEmail;
                mrAndrewCencini.EmailAddress = replacementEmail;

                int recordsTouched = dbc.GetDbProxy(TestAspects).ExecuteCommand(db => db.ToStringEx(""));
                Assert.AreEqual(1, recordsTouched);
                mrAndrewCencini = dbc.GetDbProxy(TestAspects).Single(db => db.QueryCustomerByID(163));
            }

            Assert.AreEqual(mrAndrewCencini.EmailAddress, replacementEmail);

            //Customer mrAndrewCencini = new Customer { CustomerID = 163 };
            //int retVal = AwDal.ExecuteCommand(db => db.DeleteEntity(mrAndrewCencini));
            //Assert.AreEqual(1, retVal);
        }
 public IEnumerable <ProductCategoryModel> GetCategories()
 {
     using (var ctx = new AdventureWorksLT2008R2Entities())
     {
         return(_mapper.Map <List <ProductCategoryModel> >(ctx.ProductCategories.OrderBy(p => p.Name).ToList()));
     }
 }
Ejemplo n.º 4
0
        public void TestDbCtxGetOrAttach()
        {
            //int id = LinqTests.customerIdWithManyAddresses;
            //var addresses = LinqTests.AwDal.List(db => db.QueryCustomerAddressesByCustomerID(id));
            //Assert.IsTrue(2 == addresses.Count);

            //LinqTests.AwDal.Invoke(db => db.ToStringEx(""));

            Customer cust = LinqTests.AwDal.Invoke(db => db.GetOrAttach(OrhpanCust, c => c.CustomerID));

            Assert.IsNull(cust.EmailAddress);


            Customer cust2;

            using (var dbc = new AdventureWorksLT2008R2Entities())
            {
                dbc.Configuration.LazyLoadingEnabled = false;

                cust  = dbc.GetProxy(LinqTests.TestAspects).Single(db => db.QueryCustomerByID(LinqTests.CustomerIdWithManyAddresses));
                cust2 = dbc.GetProxy(LinqTests.TestAspects).Invoke(db => db.GetOrAttach(OrhpanCust));
            }
            Assert.IsNotNull(cust2.EmailAddress);
            Assert.IsTrue(cust.Equals(cust2));
        }
Ejemplo n.º 5
0
        public Tuple <int, IEnumerable <ProductModel> > GetProductsFiltered(GridFilter filter)
        {
            var skip = filter.Page * filter.PageSize;

            using (var ctx = new AdventureWorksLT2008R2Entities())
            {
                var productQuery = BuildQuery(ctx, filter);

                //var products = _mapper.Map<List<ProductModel>>(ctx.Products.ToList());

                var products = _mapper.Map <List <ProductModel> >(productQuery.ToList());
                return(new Tuple <int, IEnumerable <ProductModel> >(products.Count(), products.Skip(skip).Take(filter.PageSize)));
            }
        }
Ejemplo n.º 6
0
        public void CallPerfDbContextAugmented()
        {
            long runsPerSec;

            using (var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;

                runsPerSec = RunCounter.SpinPerSec(MillisecToRun, () =>
                                                   db.GetProxy().List(inst => inst.QueryCustomerAddressesByCustomerID(customerIdWithManyAddresses))
                                                   );
            }
            this.TestContext.WriteLine("db.QueryCustomerAddressesByCustomerID(customerIdWithManyAddresses) augmented sequential base line test result: {0} calls/second.", runsPerSec);
        }
Ejemplo n.º 7
0
        public void CallPerfDbContextDirect()
        {
            long runsPerSec;

            using (var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;

                // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                runsPerSec = RunCounter.SpinPerSec(MillisecToRun, () =>
                                                   db.QueryCustomerAddressesByCustomerID(customerIdWithManyAddresses).ToList()
                                                   );
            }
            this.TestContext.WriteLine("db.QueryCustomerAddressesByCustomerID(customerIdWithManyAddresses) direct sequential base line test result: {0} calls/second.", runsPerSec);
        }
Ejemplo n.º 8
0
        public void LinqTestOne()
        {
            IList <Address> addresses;

            // Regular EF call without AOP:
            using (var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;
                addresses = db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses).ToList();
            }
            Assert.AreEqual(2, addresses.Count);

            // Now same with LINQ-friendly AOP shortcuts:

            // Example 1: where AOP creates instance of AdventureWorksLT2008R2Entities, runs the DAL method,
            // and disposes AdventureWorksLT2008R2Entities instance - all in one shot.
            addresses = AwDal.List(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            Assert.AreEqual(2, addresses.Count);

            // Example 2: with simple AOP proxied call for existing instance of DbContext.
            using (var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;
                addresses = db.GetDbProxy(TestAspects).List(dbx => dbx.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            }
            Assert.AreEqual(2, addresses.Count);

            var address = AwDal.Single(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));

            Assert.IsNotNull(address);

            long adrressCount = AwDal.Count(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));

            Assert.AreEqual(2, adrressCount);

            adrressCount = AwDal.Count(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses), new QueryModifiers().AddSortCriteria("AddressID").AddPaging(0, 1));
            Assert.AreEqual(1, adrressCount);
        }
Ejemplo n.º 9
0
        private IEnumerable <Product> BuildQuery(AdventureWorksLT2008R2Entities ctx, GridFilter filter)
        {
            var queryFilter =
                from ft in filter.Filters
                group ft by ft.ColumnName into newGroup
                orderby newGroup.Key
                select newGroup;

            IQueryable <Product> query = ctx.Products;

            foreach (var fieldGroup in queryFilter)
            {
                var expressionBuilder = new StringBuilder();

                expressionBuilder.Append("(");
                foreach (var field in fieldGroup)
                {
                    if (expressionBuilder.Length > 1)
                    {
                        switch (field.Operator)
                        {
                        case FilterOperator.Or:
                            expressionBuilder.Append(" || ");
                            break;

                        default:
                            expressionBuilder.Append(" && ");
                            break;
                        }
                    }

                    if (field.ColumnName == "ProductCategoryName")
                    {
                        field.ColumnName = "ProductCategory.Name";
                    }

                    var expression = ExpressionBuilder.GetExpression(field.ColumnName, field.Condition, field.Value,
                                                                     _columnDictionary[field.ColumnName]);

                    expressionBuilder.Append(expression);
                }

                expressionBuilder.Append(")");

                query = query.Where(expressionBuilder.ToString());
            }

            if (!string.IsNullOrEmpty(filter.SortField))
            {
                if (filter.SortField == "ProductCategoryName")
                {
                    filter.SortField = "ProductCategoryID";
                }

                if (filter.SortOrder == FilterSortOrder.Desc)
                {
                    query = query.OrderBy(filter.SortField + " DESC");
                }
                else
                {
                    query = query.OrderBy(filter.SortField);
                }
            }

            return(query);
        }
 public SqlUnitOfWork()
 {
     _context = new AdventureWorksLT2008R2Entities();
 }