public static IServiceCollection ConfigureDataContext(this IServiceCollection services, Configuration configuration) { //If this type is present - we're on mono var runningOnMono = Type.GetType("Mono.Runtime") != null; services.AddEntityFramework(configuration) .AddStore(runningOnMono) .AddDbContext<MyShuttleContext>(options => { if (runningOnMono) { options.UseInMemoryStore(); } else { options.UseSqlServer(configuration.Get("EntityFramework:MyShuttleContext:ConnectionstringKey")); } }); // Configure DbContext services.Configure<MyShuttleDbContextOptions>(options => { options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); }); return services; }
public static IServiceCollection AddSmashLeagueData( this IServiceCollection services, Action<SmashLeagueDataOptions> setup) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (setup == null) { throw new ArgumentNullException(nameof(setup)); } var dataOptions = new SmashLeagueDataOptions(); setup(dataOptions); services.AddEntityFramework() .AddSqlServer() .AddDbContext<SmashLeagueDbContext>(options => { options.UseSqlServer(dataOptions.ConnectionString); }); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<SmashLeagueDbContext>() .AddDefaultTokenProviders(); services.AddTransient<ApplicationUserManager>(); services.AddTransient<SmashLeagueDbInitializer>(); return services; }
public static IServiceCollection AddDataManagementForEntityFrameworkSqlite(this IServiceCollection services, IConfiguration configuration) { services.AddEntityFramework() .AddSqlite() .AddDbContext<ProductNameContext>(options => options.UseSqlite(configuration["Data:SqliteConnection:ConnectionString"])); services.AddTransient<IHandleProductNameData, EFProductNameDataAccessor>(); return services; }
public static IServiceCollection AddDataContext(this IServiceCollection services, IConfigurationRoot configuration) { services.AddEntityFramework() .AddSqlServer() .AddDbContext<DataContext>(options => { var str = configuration.Get<string>(key: "Data:DefaultConnection:ConnectionString"); options.UseSqlServer(str); }); services.AddScoped<IDataContext>(x => x.GetRequiredService<DataContext>()); return services; }
public static IServiceCollection ConfigureDataContext(this IServiceCollection services, IConfiguration configuration) { var runningOnMono = Type.GetType("Mono.Runtime") != null; bool useInMemoryStore = runningOnMono || configuration["Data:UseInMemoryStore"].Equals("true", StringComparison.OrdinalIgnoreCase); services.AddEntityFramework() .AddStore(useInMemoryStore) .AddDbContext<MyShuttleContext>(options => { options.UseSqlServer(configuration["Data:DefaultConnection:Connectionstring"]); }); return services; }
public static IServiceCollection AddPersistenceServices(this IServiceCollection services) { var config = services.BuildServiceProvider().GetRequiredService<IConfigurationRoot>(); string connectionString = config["Data:ConnectionString"]; services.AddEntityFramework() .AddSqlite() .AddDbContext<Context>(options => options.UseSqlite(connectionString)); var database = services.BuildServiceProvider().GetService<Context>().Database; //https://github.com/aspnet/EntityFramework/issues/3160 //EnsureCreated is designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. //If you are using migrations and want to have them automatically applied on app start, then you can use context.Database.Migrate() instead //database.EnsureCreated(); database.Migrate(); return services; }
/// <summary> /// Adds an IBookStore implementation to services depending on configuration. /// </summary> public static void AddBookStore(this IServiceCollection services, IConfigurationRoot configuration, ILoggerFactory loggerFactory) { var logger = loggerFactory.CreateLogger("AddBookStore"); EntityFrameworkServicesBuilder entityFramework; // Choose a a backend to store the books. switch (configuration["Data:BookStore"]?.ToLower()) { case "sqlserver": logger.LogInformation("Storing book data in SQL Server."); entityFramework = services.AddEntityFramework(); string sqlserverConnectionString = configuration.GetOrThrow("Data:SqlServer:ConnectionString"); entityFramework.AddSqlServer() .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(sqlserverConnectionString)); services.AddScoped(typeof(IBookStore), typeof(DbBookStore)); break; case "postgres": logger.LogInformation("Storing book data in PostgreSql."); entityFramework = services.AddEntityFramework(); string npgsqlConnectionString = configuration.GetOrThrow("Data:Npgsql:ConnectionString"); entityFramework.AddNpgsql() .AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(npgsqlConnectionString)); services.AddScoped(typeof(IBookStore), typeof(DbBookStore)); break; case "datastore": logger.LogInformation("Storing book data in Datastore."); services.AddSingleton<IBookStore>((IServiceProvider provider) => { return new DatastoreBookStore( configuration.GetOrThrow("GOOGLE_PROJECT_ID")); }); break; default: throw new ConfigurationException("No bookstore backend selected.\n" + "Set the configuration variable Data:BookStore to " + "one of the following: sqlserver, postgres, datastore."); } }
public static IServiceCollection ConfigureDataContext(this IServiceCollection services, IConfiguration configuration, bool useInMemoryStore) { services.AddEntityFramework() .AddStore(useInMemoryStore) .AddDbContext<MyHealthContext>(options => { if (useInMemoryStore) { options.UseInMemoryDatabase(); } else { options.UseSqlServer(configuration["Data:DefaultConnection:Connectionstring"]); } }); return services; }
public static IServiceCollection ConfigureDataContext(this IServiceCollection services, IConfiguration configuration) { //If this type is present - we're on mono var runningOnMono = Type.GetType("Mono.Runtime") != null; services.AddEntityFramework() .AddStore(runningOnMono) .AddDbContext<MyShuttleContext>(options => { if (runningOnMono) { options.UseInMemoryStore(); } else { options.UseSqlServer(configuration.Get("Data:DefaultConnection:Connectionstring")); } }); return services; }
public static IServiceCollection AddEntityFrameworkMyProvider(this IServiceCollection services) { services.AddEntityFramework(); services.TryAddEnumerable(ServiceDescriptor .Singleton<IDatabaseProvider, DatabaseProvider<MyDatabaseProviderServices, MyProviderOptionsExtension>>()); services.TryAdd(new ServiceCollection() // singleton services .AddSingleton<MyModelSource>() .AddSingleton<MyValueGeneratorCache>() // scoped services .AddScoped<MyDatabaseProviderServices>() .AddScoped<MyDatabaseCreator>() .AddScoped<MyDatabase>() .AddScoped<MyEntityQueryableExpressionVisitorFactory>() .AddScoped<MyEntityQueryModelVisitorFactory>() .AddScoped<MyQueryContextFactory>() .AddScoped<MyTransactionManager>()); return services; }
public static void AddHermesDataContext(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } // @TODO get strongly typed options from configuration instead of magic strings //Tells us how to connect to the db var defaultConnectionOptions = configuration.GetSection("DefaultConnection"); var settings = new DataAccessOptions(); settings.DefaultConnection.ConntectionString = defaultConnectionOptions["ConnectionString"]; DatabaseType dbType; if (Enum.TryParse<DatabaseType>(defaultConnectionOptions["DatabaseType"], out dbType)) { settings.DefaultConnection.Type = dbType; } //connects to the db if (settings.DefaultConnection.Type == DatabaseType.MsSqlServer) { services.AddEntityFramework() .AddSqlServer() .AddDbContext<DataContext>(options => { options.UseSqlServer(settings.DefaultConnection.ConntectionString); }); services.AddScoped<IDataContext, DataContext>(); } else { throw new NotImplementedException($"Database type ({settings.DefaultConnection.Type}) not yet implemented."); } }