我想要实现的
我想为我的应用程序创建一个ValueConverter,以便能够加密数据库中的字符串值。我知道这是存在的,我更愿意自己做。
我遇到的问题
当我试图调用我的上下文时,我得到了一个System.NullReferenceException: Object reference not set to an instance of an object.错误。我尝试过删除属性、重写构造函数、更改静态方法,但都没有成功。
代码
值转换器
public class AesGcmConverter : ValueConverter<string, string>
{
public AesGcmConverter(ConverterMappingHints mappingHints = default) : base(EncryptExpr, DecryptExpr, mappingHints)
{
}
static Expression<Func<string, string>> DecryptExpr = x => new string(x.Reverse().ToArray());
static Expression<Func<string, string>> EncryptExpr = x => new string(x.Reverse().ToArray());
}EF
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
var attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptedAttribute), false);
if (attributes.Any())
{
property.SetValueConverter(new AesGcmConverter());
}
}
}
}发布于 2022-07-09 20:39:07
你在哪条线上发现异常?提示:也许数据库中有null string值,那么调用x.Reverse()会在x is null时抛出NullReferenceObject异常
https://stackoverflow.com/questions/67888529
复制相似问题