public SqlServerRepository(string connectionString)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Config>());

            context = new DataContext(connectionString);
        }
예제 #2
0
        public static void ExportReportToJsonFiles(DataContext data)
        {
            var products = data.Products.Select(p => new
            {
                id = p.Id,
                name = p.Name,
                productType = p.ProductType,
                ingerdients = p.Ingredients,
                unitType = p.UnitType,
                qunatity = p.Quantity
            });

            Array.ForEach(Directory.GetFiles("..\\..\\..\\Reports\\Json-Reports\\"), File.Delete);

            var serializer = new JsonSerializer();
            foreach (var product in products)
            {
                string path = "..\\..\\..\\Reports\\Json-Reports\\" + product.id + ".json";

                using (var fileStream = new FileStream(path, FileMode.CreateNew))
                {
                    using (var sw = new StreamWriter(fileStream))
                    {
                        using (var writer = new JsonTextWriter(sw))
                        {
                            writer.Formatting = Formatting.Indented;
                            serializer.Serialize(writer, product);
                        }
                    }
                }
            }
        }