Skip to content

Set of useful extensions for EF Core (Dynamic data masking, MigrationBuilder and ModelBuilder extensions, etc)

License

Notifications You must be signed in to change notification settings

nikitasavinov/EntityFrameworkCore.Extensions

Repository files navigation

EntityFrameworkCore.Extensions

Please refer to EntityFrameworkCore.Extensions.Samples for usage examples.

Build status

Build Codacy Badge NugetPackage

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

About

Set of useful extensions for EF Core (Dynamic data masking, MigrationBuilder and ModelBuilder extensions, etc)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages