Please refer to EntityFrameworkCore.Extensions.Samples for usage examples.
public class SampleContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//1. Use EntityFrameworkCoreExtensions (add DynamicDataMasking support)
optionsBuilder.UseEntityFrameworkCoreExtensions();
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//2. Set Cascade as a default delete behaviour
modelBuilder.OverrideDeleteBehaviour(DeleteBehavior.Cascade);
//3. Add dynamic data masking (https://docs.microsoft.com/en-us/sql/relational-databases/security/dynamic-data-masking)
modelBuilder.Entity<Customer>().Property(t => t.Surname).HasDataMask(MaskingFunctions.Default());
modelBuilder.Entity<Customer>().Property(t => t.DiscountCardNumber).HasDataMask(MaskingFunctions.Random(10, 100));
modelBuilder.Entity<Customer>().Property(t => t.Phone).HasDataMask(MaskingFunctions.Partial(2, "XX-XX", 1));
}
public DbSet<Customer> Customers { get; set; }
}
static void Main(string[] args)
{
using (var context = new SampleContext())
{
//4. Will not throw when UseInMemoryDatabase is used
context.Database.MigrateIfSupported();
}
}