Eastrall / EntityFrameworkCore.DataEncryption

A plugin for Microsoft.EntityFrameworkCore to add support of encrypted fields using built-in or custom encryption providers.
MIT License
326 stars 54 forks source link

Require un-generic type of func "IsEncrypted()" in PropertyBuilderExtensions #47

Closed feast107 closed 1 year ago

feast107 commented 1 year ago

Is it undecided to provide or may it lead to some unknown error if provided? I have now written a required version in my code. For I need to use reflection to register property automatically.

Eastrall commented 1 year ago

Can you provide a sample code about what you are trying to achieve please?

feast107 commented 1 year ago

In onModelCreating it will be called like:

modelBuild.Entity<TEntity>.AllPropertiesExcept(new []
{
    nameof(TEntity.NotEncryptedProperty),
    ...
}).IsEncrypted();  //AreEncrypted()...?

AllPropertiesExcept returns IEnumerable<PropertyBuilder> for it uses reflection, which makes me cannot use generic-type of PropertyBuilder<TProperty>. I'v written a

PropertyBuilder IsEncrypted(this PropertyBuilder builder,StorageFormat storageFormat = StorageFormat.Default)

use the same implement of IsEncrypted<TProperty> in this repo and it seems work well. Thanks very much anyway! :satisfied:

feast107 commented 1 year ago

I don't mind contributing these codes if u need them, IsEncrypted<TProperty> seems lonely in the extension. :yum:

Eastrall commented 1 year ago

Since your method is returning a IEnumerable<PropertyBuilder>, you can iterate over this collection and call the IsEncrypted() method for each element:

IEnumerable<PropertyBuilder> encryptedProperties = modelBuilder.Entity<TEntity>.AllPropertiesExcept(new[]
{
    nameof(TEntity.NotEncryptedProperty),
  // ...
});

foreach (IPropertyBuilder property in encryptedProperties)
{
    property.IsEncrypted();
}

I don't think there is a point in doing a AreEncrypted() method inside the library, but you can still build this extensions for your project if needed.