Пример #1
0
        private static void FromMySqlNeverAgain()
        {
            var dbcontext = new SupermarketContext();

            var mySqlContx = new SupermarketModel();
            using (mySqlContx)
            {
                var products = mySqlContx.Products.OrderBy(e => e.ID).ToList();
                var vendors = mySqlContx.Vendors.ToList();
                var mesuares = mySqlContx.Measures.ToList();
                using (dbcontext)
                {

                    foreach (var mesuare in mesuares)
                    {
                        var newMeasure = new Measure()
                        {
                            ID = mesuare.ID,
                            Name = mesuare.Name
                        };
                        dbcontext.Measures.Add(newMeasure);

                    }

                    foreach (var vendor in vendors)
                    {
                        var newVendor = new Vendor()
                        {
                            ID = vendor.ID,
                            Name = vendor.Name
                        };
                        dbcontext.Vendors.Add(newVendor);
                    }


                    foreach (var product in products)
                    {
                        var some = new Product
                        {
                            BasePrice = product.BasePrice,
                            Measure_ID = product.Measure_ID,
                            Name = product.Name,
                            Vendor_ID = product.Vendor_ID,
                        };
                        dbcontext.Products.Add(some);
                    }

                    dbcontext.SaveChanges();
                }


            }
        }
Пример #2
0
        private static void InitializeDataFromMySQL()
        {
            var db = new SupermarketContext();
            SupermarketModel model = new SupermarketModel();

            using (db)
            {
                foreach (var measure in model.Measures)
                {
                    db.Measurments.Add(new Measure() {
                        Id = measure.Id, 
                        MeasureName = measure.MeasureName
                    });
                }

                foreach (var vendor in model.Vendors)
                {

                    db.Vendors.Add(new Vendor() { 
                        Id = vendor.Id,
                        VendorName = vendor.VendorName
                    });
                }
                foreach (var product in model.Products)
                {
                    db.Products.Add(new Product() {
                        Id = product.Id, 
                        BasePrice = product.BasePrice,
                        MeasureId=product.MeasureId,
                        ProductName = product.ProductName, 
                        VendorId= product.VendorId
                    });
                }

                db.SaveChanges();
                Console.WriteLine("1. Data from MySQL saved in SQL Server!");
            }
        }