Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions EncryptColumnTest/Context/ExampleDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EncryptColumnTest.Models;
using EntityFrameworkCore.EncryptColumn.Interfaces;
using EntityFrameworkCore.EncryptColumn.Util;
using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.EncryptColumn.Extension;

namespace EncryptColumnTest.Context;

public class ExampleDbContext:DbContext
{
private readonly IEncryptionProvider _provider;
public ExampleDbContext()
{
this._provider = new GenerateEncryptionProvider("example_encrypt_key_must be 32 b");
}

public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=EncryptColumnTest;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.UseEncryption(this._provider);
//modelBuilder.HasDefaultSchema("dbo");
//modelBuilder.Entity<User>(entity =>
//{
// entity.ToTable("Users");
// entity.HasKey(u => u.ID).HasName("PK_User_Id");
// entity.Property(p => p.ID).HasColumnType("GUID").IsRequired().ValueGeneratedOnAdd();
//});
}
}

58 changes: 58 additions & 0 deletions EncryptColumnTest/EncryptColumnTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[6.0.16,7.0.0)" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[7.0.5,8.0.0)" Condition="'$(TargetFramework)' == 'net7.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="[8.0.0, 9.0.0)" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="[6.0.16,7.0.0)" Condition="'$(TargetFramework)' == 'net6.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="[7.0.5,8.0.0)" Condition="'$(TargetFramework)' == 'net7.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="[8.0.0, 9.0.0)" Condition="'$(TargetFramework)' == 'net8.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[6.0.16,7.0.0)" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[7.0.0,8.0.0)" Condition="'$(TargetFramework)' == 'net7.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="[8.0.0, 9.0.0)" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="[6.0.0,7.0.0)" Condition="'$(TargetFramework)' == 'net6.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="[7.0.5, 8.0.0)" Condition="'$(TargetFramework)' == 'net7.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="[8.0.0,9.0.0)" Condition="'$(TargetFramework)' == 'net8.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EntityFramework.Core.EncryptDBColumn\EntityFrameworkCore.EncryptColumn.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions EncryptColumnTest/Migrations/20220314125216_initial.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions EncryptColumnTest/Migrations/20220314125216_initial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace EncryptColumnTest.Migrations
{
public partial class initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
ID = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Firstname = table.Column<string>(type: "nvarchar(max)", nullable: false),
Lastname = table.Column<string>(type: "nvarchar(max)", nullable: false),
EmailAddress = table.Column<string>(type: "nvarchar(max)", nullable: false),
IdentityNumber = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.ID);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}
54 changes: 54 additions & 0 deletions EncryptColumnTest/Migrations/ExampleDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <auto-generated />
using System;
using EncryptColumnTest.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace EncryptColumnTest.Migrations
{
[DbContext(typeof(ExampleDbContext))]
partial class ExampleDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("EncryptColumnTest.Models.User", b =>
{
b.Property<Guid>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<string>("EmailAddress")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Firstname")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("IdentityNumber")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Lastname")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("ID");

b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
23 changes: 23 additions & 0 deletions EncryptColumnTest/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using EntityFrameworkCore.EncryptColumn.Attribute;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EncryptColumnTest.Models;

public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? ID { get; set; }
public string? Firstname { get; set; }
public string? Lastname { get; set; }
[EncryptColumn]
public string? EmailAddress { get; set; }
[EncryptColumn]
public string? IdentityNumber { get; set; }
}
95 changes: 95 additions & 0 deletions EncryptColumnTest/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using EncryptColumnTest.Context;
using EncryptColumnTest.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace EncryptColumnTest;
[TestClass]
public class UnitTest1
{
private static ExampleDbContext testContext=new();
private User testUser = new User
{
Firstname = "Emre",
Lastname = "Kizildas",
EmailAddress = "kizildas@icloud.com",
IdentityNumber = "12345678901"
};

public UnitTest1()
{
//testContext=new();
}

//[ClassInitialize()]
//public static void Initialize()
// {
// testContext.Users.RemoveRange(testContext.Users); // clear the database.
// }

[TestMethod]
public void AddRecord()
{
try
{
testContext.Users.Add(testUser);
testContext.SaveChanges();
}
catch(Exception Ex)
{
Assert.Fail($"Exception thrown on {nameof(AddRecord)}: {Ex.Message}");
}
}

[TestMethod]
public void ReadRecordByName()
{
try
{
var user = testContext.Users.FirstOrDefault(u => u.Firstname == testUser.Firstname);
Assert.IsNotNull(user);
Assert.IsNotNull(user.Firstname);
Assert.AreEqual(testUser.Firstname, user.Firstname);
Assert.AreEqual(testUser.IdentityNumber, user.IdentityNumber);
Assert.AreEqual(testUser.EmailAddress, user.EmailAddress);
}
catch(Exception Ex)
{
Assert.Fail($"Exception thrown on {nameof(ReadRecordByName)}: {Ex.Message}");
}
}

[TestMethod]
public void ReadRecordByIdentityNumber()
{
try
{
var user = testContext.Users.FirstOrDefault(u => u.IdentityNumber == testUser.IdentityNumber);
Assert.IsNotNull(user);
Assert.IsNotNull(user.Firstname);
Assert.AreEqual("Emre", user.Firstname);
Assert.AreEqual("12345678901", user.IdentityNumber);
Assert.AreEqual("kizildas@icloud.com", user.EmailAddress);
}
catch(Exception Ex)
{
Assert.Fail($"Exception thrown on {nameof(ReadRecordByName)}: {Ex.Message}");
}
}

[TestMethod]
public void AddAnotherRecord()
{
try
{
User newUser = new User { EmailAddress="jdubois31@outlook.com", Firstname="John", Lastname="Dubois", IdentityNumber="299792458" };
testContext.Users.Add(newUser);
testContext.SaveChanges();
}
catch (Exception Ex)
{
Assert.Fail($"Exception thrown on {nameof(AddAnotherRecord)}: {Ex.Message}");
}
}
}
Loading