public void OneTimeSetUp()
        {
            var configuration = new Configuration();

            configuration.TargetDatabase = new DbConnectionInfo("DefaultConnection");

            var migrator = new DbMigrator(configuration);

            migrator.Update();

            var doc      = XDocument.Load(Path.Combine("TestData", "TestData.xml"));
            var import   = new XmlImport(doc, "http://tempuri.org/Database.xsd");
            var customer = new List <Customer>();

            using (var session = new CoreDbContext())
            {
                import.Parse(new[] { typeof(Customer), typeof(Order), typeof(Product) },
                             (type, obj) =>
                {
                    switch (type.Name)
                    {
                    case nameof(Customer):
                        session.Customers.Add((Customer)obj);
                        break;

                    case nameof(Order):
                        session.Orders.Add((Order)obj);
                        break;

                    case nameof(Product):
                        session.Products.Add((Product)obj);
                        break;

                    default:
                        break;
                    }
                });
                session.SaveChanges();
            }
            using (var session = new CoreDbContext())
            {
                import.ParseConnections("OrderProduct", "Product", "Order", (productId, orderId) =>
                {
                    var product = session.Products.Single(p => p.Id == productId);
                    var order   = session.Orders.Single(o => o.Id == orderId);
                    session.ProductOrders.Add(new ProductOrder {
                        Order = order, Product = product
                    });
                });

                import.ParseIntProperty("Order", "Customer", (orderId, customerId) =>
                {
                    session.Orders.Single(o => o.Id == orderId).Customer = session.Customers.Single(c => c.Id == customerId);
                });

                session.SaveChanges();
            }
        }
示例#2
0
        public static DbContextOptions Setup(DbContextOptions options)
        {
            var doc = XDocument.Load(Path.Combine(
                                         Path.GetDirectoryName(typeof(CustomerDataTests).GetTypeInfo().Assembly.Location),
                                         "TestData", "TestData.xml"));
            var import   = new XmlImport(doc, "http://tempuri.org/Database.xsd");
            var customer = new List <Customer>();

            using (var session = new CoreDbContext(options))
            {
                import.Parse(new[] { typeof(Customer), typeof(Order), typeof(Product) },
                             (type, obj) =>
                {
                    switch (type.Name)
                    {
                    case nameof(Customer):
                        session.Customers.Add((Customer)obj);
                        break;

                    case nameof(Order):
                        session.Orders.Add((Order)obj);
                        break;

                    case nameof(Product):
                        session.Products.Add((Product)obj);
                        break;

                    default:
                        break;
                    }
                });
                session.SaveChanges();
            }
            using (var session = new CoreDbContext(options))
            {
                import.ParseConnections("OrderProduct", "Product", "Order", (productId, orderId) =>
                {
                    var product = session.GetProduct(productId);
                    var order   = session.GetOrder(orderId);
                    session.ProductOrders.Add(new ProductOrder {
                        Order = order, Product = product
                    });
                });

                import.ParseIntProperty("Order", "Customer", (orderId, customerId) =>
                {
                    var order      = session.GetOrder(orderId);
                    order.Customer = session.GetCustomer(customerId);
                });

                session.SaveChanges();
            }
            return(options);
        }