-
Notifications
You must be signed in to change notification settings - Fork 2
Entity Framework Core Database Provider
Teuz edited this page Apr 3, 2025
·
2 revisions
For easy integration with Entity Framework Core, you can use the FeatureManagement.Database.EntityFrameworkCore package.
This package provides:
- A default, extendable
FeatureManagementDbContextwith pre-configured entities for features and feature settings. - A default
FeatureStoreimplementation of theIFeatureStoreinterface, which can be extended as needed.
First, install the package:
dotnet add package FeatureManagement.Database.EntityFrameworkCoreThen install the specific database provider package:
# For SQL Server
dotnet add package FeatureManagement.Database.EntityFrameworkCore.SqlServer
# For PostgreSQL
dotnet add package FeatureManagement.Database.EntityFrameworkCore.PostgreSQL
# For SQLite
dotnet add package FeatureManagement.Database.EntityFrameworkCore.Sqlite
# For MySQL
dotnet add package FeatureManagement.Database.EntityFrameworkCore.MySqlConfigure the services with the database provider you want:
services.AddDatabaseFeatureManagement<FeatureStore>()
.ConfigureDbContext<FeatureManagementDbContext>(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));Using EF Core, you can work with different database providers which provide an extension method to the services.AddDatabaseFeatureManagement<FeatureStore>():
| Database Provider | Package | Extension method |
|---|---|---|
| SQL Server | FeatureManagement.Database.EntityFrameworkCore.SqlServer |
UseSqlServer<FeatureManagementDbContext>(...); |
| PostgreSQL | FeatureManagement.Database.EntityFrameworkCore.PostgreSQL |
UseNpgsql<FeatureManagementDbContext>(...); |
| Sqlite | FeatureManagement.Database.EntityFrameworkCore.Sqlite |
UseSqlite<FeatureManagementDbContext>(...); |
| MySql | FeatureManagement.Database.EntityFrameworkCore.MySql |
UseMySql<FeatureManagementDbContext>(...); |
If you already have an existing DbContext and want to integrate it with EF Core:
- Create a custom DbContext that inherits from
FeatureManagementDbContext:
public class MyDbContext : FeatureManagementDbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
// Your existing DbSets and configurations
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Your custom entity configurations
modelBuilder.Entity<MyEntity>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired();
});
}
}- Create a custom feature store that uses your DbContext:
public class MyFeatureStore : FeatureStore<MyDbContext>
{
public MyFeatureStore(MyDbContext dbContext) : base(dbContext)
{
}
// Optionally override methods for custom behavior
public override async Task<Feature> GetFeatureAsync(string featureName)
{
// Custom implementation or logging
return await base.GetFeatureAsync(featureName);
}
}- Register your services:
services.AddDbContext<MyDbContext>(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseFeatureManagement<MyFeatureStore>();Note
When using a custom DbContext, ensure that MyFeatureStore also extends the default one to utilize the custom DbContext.
To create and run migrations for the feature management tables:
# Create a migration
dotnet ef migrations add AddFeatureManagementTables -c MyDbContext
# Apply migrations
dotnet ef database update -c MyDbContextpublic void ConfigureServices(IServiceCollection services)
{
// Register EF Core with SQL Server
services.AddDatabaseFeatureManagement<FeatureStore>()
.UseSqlServer<FeatureManagementDbContext>(options =>
{
options.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
options.MigrationsAssembly = typeof(Startup).Assembly.FullName;
options.EnableRetryOnFailure(3);
})
.WithCacheService(options =>
{
options.SlidingExpiration = TimeSpan.FromMinutes(5);
});
// Other service registrations...
}