Skip to content

Commit 5fd4287

Browse files
committed
Breaking:
1. Removed `DynamicDataMasking/DataMaskingAttribute.cs` (implementation was broken) 2. Moved DynamicDataMasking/MaskingFunctions.cs to the `EntityFrameworkCore.Extensions` namespace Added a few useful functions (`UseEntityFrameworkCoreExtensions`, `HasDataMask`)
1 parent 0a75f97 commit 5fd4287

14 files changed

Lines changed: 143 additions & 61 deletions

EntityFrameworkCore.Extensions.Samples/EntityFrameworkCore.Extensions.Samples.csproj

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,61 @@
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<Compile Remove="Migrations\20200325195611_AddColumn.cs" />
10+
<Compile Remove="Migrations\20200325195611_AddColumn.Designer.cs" />
11+
<Compile Remove="Migrations\20200325195829_RemoveColumn.cs" />
12+
<Compile Remove="Migrations\20200325195829_RemoveColumn.Designer.cs" />
13+
<Compile Remove="Migrations\20200325195948_RemoveColumn.cs" />
14+
<Compile Remove="Migrations\20200325195948_RemoveColumn.Designer.cs" />
15+
<Compile Remove="Migrations\20200325202110_RemoveColumn.cs" />
16+
<Compile Remove="Migrations\20200325202110_RemoveColumn.Designer.cs" />
17+
<Compile Remove="Migrations\20200325202129_RemoveColumn2.cs" />
18+
<Compile Remove="Migrations\20200325202129_RemoveColumn2.Designer.cs" />
19+
<Compile Remove="Migrations\20200325202644_RemoveColumn4.cs" />
20+
<Compile Remove="Migrations\20200325202644_RemoveColumn4.Designer.cs" />
21+
<Compile Remove="Migrations\20200328124135_Remove.cs" />
22+
<Compile Remove="Migrations\20200328124135_Remove.Designer.cs" />
23+
<Compile Remove="Migrations\20200328124236_Remove.cs" />
24+
<Compile Remove="Migrations\20200328124236_Remove.Designer.cs" />
25+
<Compile Remove="Migrations\20200328124611_Remove123.cs" />
26+
<Compile Remove="Migrations\20200328124611_Remove123.Designer.cs" />
27+
<Compile Remove="Migrations\20200328132328_test.cs" />
28+
<Compile Remove="Migrations\20200328132328_test.Designer.cs" />
29+
<Compile Remove="Migrations\20200328135359_test1.cs" />
30+
<Compile Remove="Migrations\20200328135359_test1.Designer.cs" />
31+
<Compile Remove="Migrations\20200328142140_test1234.cs" />
32+
<Compile Remove="Migrations\20200328142140_test1234.Designer.cs" />
33+
<Compile Remove="Migrations\20200328142203_test123456.cs" />
34+
<Compile Remove="Migrations\20200328142203_test123456.Designer.cs" />
35+
<Compile Remove="Migrations\20200328143635_test123456.cs" />
36+
<Compile Remove="Migrations\20200328143635_test123456.Designer.cs" />
37+
<Compile Remove="Migrations\20200328143704_test123456sdf.cs" />
38+
<Compile Remove="Migrations\20200328143704_test123456sdf.Designer.cs" />
39+
<Compile Remove="Migrations\20200328143729_test123456sdfdfg.cs" />
40+
<Compile Remove="Migrations\20200328143729_test123456sdfdfg.Designer.cs" />
41+
<Compile Remove="Migrations\20200328143753_test123456sdfdfgsdf.cs" />
42+
<Compile Remove="Migrations\20200328143753_test123456sdfdfgsdf.Designer.cs" />
43+
<Compile Remove="Migrations\20200328150021_Sample1.cs" />
44+
<Compile Remove="Migrations\20200328150021_Sample1.Designer.cs" />
45+
<Compile Remove="Migrations\20200328150102_Sample1.cs" />
46+
<Compile Remove="Migrations\20200328150102_Sample1.Designer.cs" />
47+
<Compile Remove="Migrations\20200328150210_Sample2.cs" />
48+
<Compile Remove="Migrations\20200328150210_Sample2.Designer.cs" />
49+
<Compile Remove="Migrations\20200328151944_Sample2.cs" />
50+
<Compile Remove="Migrations\20200328151944_Sample2.Designer.cs" />
51+
<Compile Remove="Migrations\20200328152015_Sample3.cs" />
52+
<Compile Remove="Migrations\20200328152015_Sample3.Designer.cs" />
53+
<Compile Remove="Migrations\20200328152432_Sample4.cs" />
54+
<Compile Remove="Migrations\20200328152432_Sample4.Designer.cs" />
55+
<Compile Remove="Migrations\20200328152453_Sample6.cs" />
56+
<Compile Remove="Migrations\20200328152453_Sample6.Designer.cs" />
57+
<Compile Remove="Migrations\20200328152504_Sample8.cs" />
58+
<Compile Remove="Migrations\20200328152504_Sample8.Designer.cs" />
59+
<Compile Remove="Migrations\20200328152617_Sample1.cs" />
60+
<Compile Remove="Migrations\20200328152617_Sample1.Designer.cs" />
61+
</ItemGroup>
62+
863
<ItemGroup>
964
<ProjectReference Include="..\EntityFrameworkCore.Extensions\EntityFrameworkCore.Extensions.csproj" />
1065
</ItemGroup>
@@ -22,8 +77,4 @@
2277
</PackageReference>
2378
</ItemGroup>
2479

25-
<ItemGroup>
26-
<Folder Include="Migrations\" />
27-
</ItemGroup>
28-
2980
</Project>

EntityFrameworkCore.Extensions.Samples/Migrations/20200321204503_SampleMigration.Designer.cs renamed to EntityFrameworkCore.Extensions.Samples/Migrations/20200328194818_Sample.Designer.cs

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EntityFrameworkCore.Extensions.Samples/Migrations/20200321204503_SampleMigration.cs renamed to EntityFrameworkCore.Extensions.Samples/Migrations/20200328194818_Sample.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace EntityFrameworkCore.Extensions.Samples.Migrations
55
{
6-
public partial class SampleMigration : Migration
6+
public partial class Sample : Migration
77
{
88
protected override void Up(MigrationBuilder migrationBuilder)
99
{
@@ -13,12 +13,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
1313
{
1414
Id = table.Column<int>(nullable: false)
1515
.Annotation("SqlServer:Identity", "1, 1"),
16-
Name = table.Column<string>(nullable: true)
17-
.Annotation("DynamicDataMasking", "default()"),
16+
Name = table.Column<string>(nullable: true),
1817
Surname = table.Column<string>(nullable: true)
1918
.Annotation("DynamicDataMasking", "default()"),
20-
Phone = table.Column<string>(nullable: true)
21-
.Annotation("DynamicDataMasking", "partial(2, \"XX-XX\", 1)"),
19+
Surname2 = table.Column<string>(nullable: true),
20+
Phone = table.Column<string>(nullable: true),
2221
DiscountCardNumber = table.Column<int>(nullable: false)
2322
.Annotation("DynamicDataMasking", "random(10, 100)"),
2423
SampleProperty1 = table.Column<string>(nullable: true),

EntityFrameworkCore.Extensions.Samples/Migrations/SampleContextModelSnapshot.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
3434
.HasColumnType("nvarchar(max)");
3535

3636
b.Property<string>("Phone")
37-
.HasColumnType("nvarchar(max)")
38-
.HasAnnotation("DynamicDataMasking", "partial(2, \"XX-XX\", 1)");
37+
.HasColumnType("nvarchar(max)");
3938

4039
b.Property<string>("SampleProperty1")
4140
.HasColumnType("nvarchar(max)");
@@ -47,6 +46,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4746
.HasColumnType("nvarchar(max)")
4847
.HasAnnotation("DynamicDataMasking", "default()");
4948

49+
b.Property<string>("Surname2")
50+
.HasColumnType("nvarchar(max)");
51+
5052
b.HasKey("Id");
5153

5254
b.ToTable("Customers");

EntityFrameworkCore.Extensions.Samples/Models.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3-
using EntityFrameworkCore.Extensions.DynamicDataMasking;
43

54
namespace EntityFrameworkCore.Extensions.Samples
65
{
76
public class Customer
87
{
98
public int Id { get; set; }
10-
11-
[DataMasking(MaskingFunction = "default()")]
129
public string Name { get; set; }
1310
public string Surname { get; set; }
11+
public string Surname2 { get; set; }
1412
public string Phone { get; set; }
1513
public int DiscountCardNumber { get; set; }
1614
public string SampleProperty1 { get; set; }

EntityFrameworkCore.Extensions.Samples/Program.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using EntityFrameworkCore.Extensions.DynamicDataMasking;
5-
using EntityFrameworkCore.Extensions.Services;
63
using Microsoft.EntityFrameworkCore;
7-
using Microsoft.EntityFrameworkCore.Migrations;
84

95
namespace EntityFrameworkCore.Extensions.Samples
106
{
@@ -18,8 +14,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1814
{
1915
optionsBuilder.UseSqlServer("Data Source =.; Initial Catalog = EntityFrameworkCoreExtensionsSamples; Integrated Security = True;");
2016
}
21-
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, ExtendedMigrationSqlServerGenerator>(); //Add the support for DynamicDataMasking
22-
optionsBuilder.ReplaceService<IMigrationsAnnotationProvider, ExtendedSqlServerMigrationsAnnotationProvider>();
17+
optionsBuilder.UseEntityFrameworkCoreExtensions();
2318

2419
base.OnConfiguring(optionsBuilder);
2520
}
@@ -30,18 +25,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3025

3126
modelBuilder.OverrideDeleteBehaviour(DeleteBehavior.Cascade); //Set Cascade as a default delete behaviour
3227

33-
modelBuilder.Entity<Customer>().Property(t => t.Surname).HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Default());
34-
modelBuilder.Entity<Customer>().Property(t => t.DiscountCardNumber).HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Random(10, 100));
35-
modelBuilder.Entity<Customer>().Property(t => t.Phone).HasAnnotation(AnnotationConstants.DynamicDataMasking, MaskingFunctions.Partial(2, "XX-XX", 1));
36-
}
28+
modelBuilder.Entity<Customer>().Property(t => t.Surname).HasDataMask(MaskingFunctions.Default());
29+
modelBuilder.Entity<Customer>().Property(t => t.DiscountCardNumber).HasDataMask(MaskingFunctions.Random(10, 100));
30+
modelBuilder.Entity<Customer>().Property(t => t.Phone).HasDataMask(MaskingFunctions.Partial(2, "XX-XX", 1));
31+
}
3732

3833
public DbSet<Customer> Customers { get; set; }
3934
}
4035

4136
static void Main(string[] args)
4237
{
4338
using (var context = new SampleContext())
44-
{
39+
{
4540
context.Database.MigrateIfSupported(); //Will not throw when UseInMemoryDatabase is used
4641

4742
//Add a customer
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using EntityFrameworkCore.Extensions.Services;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Migrations;
5+
using System;
6+
using Xunit;
7+
8+
namespace EntityFrameworkCore.Extensions.Tests
9+
{
10+
public class DbContextOptionsBuilderExtensionsTest
11+
{
12+
[Fact]
13+
public void UseEntityFrameworkCoreExtensions_should_register_necessary_services()
14+
{
15+
var builder = new DbContextOptionsBuilder<TestContext>()
16+
.UseSqlServer("fake")
17+
.UseEntityFrameworkCoreExtensions()
18+
.Options;
19+
var context = (IInfrastructure<IServiceProvider>)new TestContext(builder);
20+
21+
var migrationSqlGenerator = context.GetService<IMigrationsSqlGenerator>();
22+
var migrationAnnotationsProvider = context.GetService<IMigrationsAnnotationProvider>();
23+
24+
Assert.IsType<ExtendedMigrationSqlServerGenerator>(migrationSqlGenerator);
25+
Assert.IsType<ExtendedSqlServerMigrationsAnnotationProvider>(migrationAnnotationsProvider);
26+
}
27+
}
28+
}

EntityFrameworkCore.Extensions.Tests/MigrationBuilderExtensionsTest.cs renamed to EntityFrameworkCore.Extensions.Tests/MigrationBuilderExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace EntityFrameworkCore.Extensions.Tests
77
{
8-
public class MigrationBuilderExtensionsTest
8+
public class MigrationBuilderExtensionsTests
99
{
1010
[Fact]
1111
public void SqlFile_should_include_sql_in_migration()

EntityFrameworkCore.Extensions.Tests/DbContextOptionsBuilderExtensionsTests.cs renamed to EntityFrameworkCore.Extensions.Tests/TestContext.cs

File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using EntityFrameworkCore.Extensions.Services;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace EntityFrameworkCore.Extensions
6+
{
7+
public static class DbContextOptionsBuilderExtensions
8+
{
9+
/// <summary>
10+
/// Register services necessary for enabling dynamic data masking (and more features in the future)
11+
/// </summary>
12+
public static DbContextOptionsBuilder UseEntityFrameworkCoreExtensions(this DbContextOptionsBuilder optionsBuilder)
13+
{
14+
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, ExtendedMigrationSqlServerGenerator>();
15+
optionsBuilder.ReplaceService<IMigrationsAnnotationProvider, ExtendedSqlServerMigrationsAnnotationProvider>();
16+
17+
return optionsBuilder;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)