Skip to content

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 FeatureManagementDbContext with pre-configured entities for features and feature settings.
  • A default FeatureStore implementation of the IFeatureStore interface, which can be extended as needed.

Installation

First, install the package:

dotnet add package FeatureManagement.Database.EntityFrameworkCore

Then 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.MySql

Basic Setup

Configure the services with the database provider you want:

services.AddDatabaseFeatureManagement<FeatureStore>()
    .ConfigureDbContext<FeatureManagementDbContext>(builder => 
        builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

Database Provider Extension Methods

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>(...);

Integration with Existing DbContext

If you already have an existing DbContext and want to integrate it with EF Core:

  1. 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();
        });
    }
}
  1. 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);
    }
}
  1. 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.

Migration Examples

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 MyDbContext

Complete Configuration Example

public 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...
}

Clone this wiki locally