Пример #1
0
        static void RecodeFromMongoDbToSQL(MongoDatabase mongoDb, DbContext sqlDbContext)
        {
            //SQL DB server
            Database.SetInitializer(
                new MigrateDatabaseToLatestVersion<SupermarketTWDbContext, Configuration>());
            var sqlDb = sqlDbContext as SupermarketTWDbContext;

            //Mongo DB server
            var products = mongoDb.GetCollection<ProductDocument>("Products").FindAll();

            foreach (var product in products)
            {
                if (sqlDb.Products.FirstOrDefault(p => p.Name.Equals(product.Name)) == null)
                {
                    var measure = sqlDb.Measures.FirstOrDefault(m => m.Name.Equals(product.Measure));
                    if (measure == null)
                    {
                        measure = new Measure()
                        {
                            Name = product.Measure
                        };
                        sqlDb.Measures.Add(measure);
                    }

                    var vendor = sqlDb.Vendors.FirstOrDefault(v => v.Name.Equals(product.Vendor));
                    if (vendor == null)
                    {
                        vendor = new Vendor()
                        {
                            Name = product.Vendor
                        };
                        sqlDb.Vendors.Add(vendor);
                    }

                    var newProduct = new Product()
                    {
                        Name = product.Name,
                        BasePrice = product.BasePrice,
                        Vendor = vendor,
                        Measure = measure
                    };

                    sqlDb.Products.Add(newProduct);
                    sqlDbContext.SaveChanges();
                }
            }
        }
Пример #2
0
 public Product()
 {
     this.measure = new Measure();
     this.vendor = new Vendor();
 }