|
1 | 1 | # EntityFrameworkCore.Extensions |
2 | 2 |
|
3 | | -Please check EntityFrameworkCore.Extensions.Samples for usage examples |
| 3 | +Please refer to EntityFrameworkCore.Extensions.Samples for usage examples |
4 | 4 |
|
5 | 5 | # Build status |
6 | 6 | [](https://ci.appveyor.com/project/nikitasavinov/entityframeworkcore-extensions) |
7 | 7 |
|
| 8 | +# Examples |
| 9 | + |
| 10 | +``` csharp |
| 11 | +public class SampleContext : DbContext |
| 12 | +{ |
| 13 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 14 | + { |
| 15 | + optionsBuilder.ThrowOnQueryClientEvaluation(); //Throw when can not generate a query instead of loading everything into memory |
| 16 | + optionsBuilder.ReplaceService<IMigrationsSqlGenerator, ExtendedMigrationSqlServerGenerator>(); //Add the support for DynamicDataMasking |
| 17 | + optionsBuilder.ReplaceService<IMigrationsAnnotationProvider, ExtendedSqlServerMigrationsAnnotationProvider>(); |
| 18 | + |
| 19 | + base.OnConfiguring(optionsBuilder); |
| 20 | + } |
| 21 | + |
| 22 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 23 | + { |
| 24 | + base.OnModelCreating(modelBuilder); |
| 25 | + |
| 26 | + //Set Cascade as a default delete behaviour |
| 27 | + modelBuilder.OverrideDeleteBehaviour(DeleteBehavior.Cascade); |
| 28 | + |
| 29 | + //Add dynamic data masking (https://docs.microsoft.com/en-us/sql/relational-databases/security/dynamic-data-masking) |
| 30 | + modelBuilder.Entity<Customer>() |
| 31 | + .Property(t => t.Surname) |
| 32 | + .HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Default()); |
| 33 | + modelBuilder.Entity<Customer>() |
| 34 | + .Property(t => t.DiscountCardNumber) |
| 35 | + .HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Random(10, 100)); |
| 36 | + modelBuilder.Entity<Customer>() |
| 37 | + .Property(t => t.Phone) |
| 38 | + .HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Partial(2, "XX-XX", 1)); |
| 39 | + } |
| 40 | + |
| 41 | + public DbSet<Customer> Customers { get; set; } |
| 42 | +} |
| 43 | + |
| 44 | +public class Customer |
| 45 | +{ |
| 46 | + public int Id { get; set; } |
| 47 | + |
| 48 | + //Another way to add DynamicDataMask |
| 49 | + [DataMasking(MaskingFunction = "default()")] |
| 50 | + public string Name { get; set; } |
| 51 | +} |
| 52 | + |
| 53 | +static void Main(string[] args) |
| 54 | +{ |
| 55 | + using (var context = new SampleContext()) |
| 56 | + { |
| 57 | + context.Database.MigrateIfSupported(); //Will not throw when UseInMemoryDatabase is used |
| 58 | +
|
| 59 | + //Will throw instead of loading everything into memory |
| 60 | + var customers = context.Customers.Where(t => SomeUnsupportedFunction(t.Phone)).ToList(); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
0 commit comments