Exemple #1
0
    /// <summary>
    /// Adds value converter(<see cref="MilvaEncryptionConverter"/>) to all strings properties.
    /// </summary>
    /// <param name="modelBuilder"></param>
    /// <param name="encryptionProvider"></param>
    public static ModelBuilder UseEncryption(this ModelBuilder modelBuilder, IMilvaEncryptionProvider encryptionProvider)
    {
        if (modelBuilder is null)
        {
            throw new MilvaDeveloperException("The given model builder cannot be null");
        }

        if (encryptionProvider is null)
        {
            throw new MilvaDeveloperException("Cannot initialize encryption with a null provider.");
        }

        var encryptionConverter = new MilvaEncryptionConverter(encryptionProvider);

        foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
        {
            foreach (IMutableProperty property in entityType.GetProperties())
            {
                if (property.ClrType == typeof(string) /* && !IsDiscriminator(property)*/)
                {
                    property.SetValueConverter(encryptionConverter);
                }
            }
        }
        return(modelBuilder);
    }
 /// <summary>
 /// Creates a new <see cref="MilvaEncryptionConverter"/> instance.
 /// </summary>
 /// <param name="encryptionProvider">Encryption provider</param>
 /// <param name="mappingHints">Entity Framework mapping hints</param>
 public MilvaEncryptionConverter(IMilvaEncryptionProvider encryptionProvider, ConverterMappingHints mappingHints = null)
     : base(value => encryptionProvider.Encrypt(value), x => encryptionProvider.Decrypt(x), mappingHints)
 {
 }