Пример #1
0
        public void OrderCustomerWithDefinition()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.Customer);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(Order), typeof(OrderDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "CustomerId", "Id" }     // set OrderNo with Id
                }
                                            ));
                mapDefinitions.Register(typeof(Customer), typeof(CustomerDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Id", "Id" }     // get only Id field
                }, true
                                            ));

                var dtoQuery = query.ProjectTo <OrderDto>(mapDefinitions);
                var orders   = dtoQuery.ToList();

                Assert.AreEqual(2, orders.Count);
                Assert.IsTrue(orders.All(o => o.CustomerId == o.Id));
                Assert.IsTrue(orders.All(o => o.Customer != null && o.Customer.Name == null));
                Assert.IsTrue(orders.All(o => !o.OrderDetails.Any()));
            }
        }
Пример #2
0
        public void ManyToOneTypeMismatchCheck()
        {
            using (var context = new TestEntities()) {
                var query = context.Set <OrderDetail>().Include(od => od.Supplier);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(OrderDetail), typeof(OrderDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "OrderDetails", "Supplier" }     // set single to collection
                }, true
                                            ));

                query.ProjectTo <OrderDto>(mapDefinitions);
            }
        }
Пример #3
0
        public void OneToManyTypeMismatchCheck()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.OrderDetails);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(Order), typeof(OrderDetailDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Supplier", "OrderDetails" }     // set collection to single
                }, true
                                            ));

                query.ProjectTo <OrderDetailDto>(mapDefinitions);
            }
        }
Пример #4
0
        public void OrderDetailsWithDefinition()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.OrderDetails.Select(od => od.Supplier));

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(OrderDetail), typeof(OrderDetailDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Id", "OrderId" }     // write OrderId into Id field
                }, true
                                            ));

                var dtoQuery = query.ProjectTo <OrderDto>(mapDefinitions);
                var orders   = dtoQuery.ToList();

                Assert.AreEqual(2, orders.Count);
                Assert.IsTrue(orders.All(o => o.OrderDetails.All(od => od.Id == o.Id)));
                // suppliers should be null even if it is included, because we wanted only explicit properties.
                Assert.IsTrue(orders.All(o => o.OrderDetails.All(od => od.Supplier == null)));
            }
        }