public void ConfigureServices(IServiceCollection services)
        {
            if (ProjectEnv.IsRemoveEnv())
            {
                var azureStorageConnString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
                var azureFileShareName     = Environment.GetEnvironmentVariable("AZURE_FILE_SHARE_NAME");
                var shareClient            = new ShareClient(azureStorageConnString, azureFileShareName);
                var dirClient        = shareClient.GetDirectoryClient("schema");
                var fileClient       = dirClient.GetFileClient("Project.csdl");
                var downloadResponse = fileClient.Download();
                var stream           = downloadResponse.Value.Content;
                var reader           = new StreamReader(stream);
                File.WriteAllText(DbContextConstants.CsdlFile, reader.ReadToEnd());
            }

            DbContextGenerator generator = new DbContextGenerator();
            var contextType = generator.GenerateDbContext(DbContextConstants.CsdlFile, DbContextConstants.Name);

            var optionsBuilder   = new DbContextOptionsBuilder();
            var dbContextOptions = optionsBuilder.UseInMemoryDatabase("RapidApiDB").Options;

            //DbContext dbContext = (DbContext)Activator.CreateInstance(contextType, new object[] { dbContextOptions });
            IDataStore dataStore = (IDataStore)Activator.CreateInstance(contextType);

            var      model          = Model.GetModel();
            Assembly targetAssembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name.Contains(DbContextConstants.Name));
            var      targetTypes    = targetAssembly.DefinedTypes;

            //dbContext.Database.CreateIfNotExists();

            if (ProjectEnv.ShouldSeedData())
            {
                var dataSeeder = new DataSeeder(model, dataStore, targetTypes);
                dataSeeder.SeedData().Wait();
            }

            services.AddSingleton(this.Configuration);
            services.AddSingleton(typeof(IDataStore), dataStore);

            services.AddMvc(options => {
                options.EnableEndpointRouting = false;
                options.Conventions.Insert(0, new DynamicControllerModelConvention(model));
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .ConfigureApplicationPartManager(d => d.FeatureProviders.Add(new DynamicControllerFeatureProvider(model)));
            services.AddControllers();
            services.AddOData();
        }
Example #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            DbContextGenerator generator = new DbContextGenerator();
            var contextType = generator.GenerateDbContext(DbContextConstants.CsdlFile, DbContextConstants.Name);

            services.AddSingleton(this.Configuration);
            //services.AddDbContext<DbContext, InMemoryDbContext>(options => options.UseInMemoryDatabase(databaseName: "Hackathon2020.Poc01Db"));
            services.AddSingleton <DbContext>(sp =>
            {
                var connectionString = $"Server=(localdb)\\MSSQLLocalDB;Database={DbContextConstants.Name};Trusted_Connection=True;MultipleActiveResultSets=true";
                DbContext dbContext  = Activator.CreateInstance(contextType, new object[] { connectionString }) as DbContext;
                dbContext.Database.CreateIfNotExists();
                return(dbContext);
            });

            services.AddMvc(options => {
                options.EnableEndpointRouting = false;
                options.Conventions.Insert(0, new DynamicControllerModelConvention(Model.GetModel()));
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .ConfigureApplicationPartManager(d => d.FeatureProviders.Add(new DynamicControllerFeatureProvider(Model.GetModel())));
            services.AddControllers();
            services.AddOData();
        }
Example #3
0
 static void Main(string[] args)
 {
     Console.WriteLine("");
     DbContextGenerator generator = new DbContextGenerator();
     var context = generator.GenerateDbContext(@"SampleModel.xml");
 }