gpt4 book ai didi

c# - Audit.NET 连接对象保存问题

转载 作者:行者123 更新时间:2023-12-02 02:52:22 25 4
gpt4 key购买 nike

我正在使用 Audit.NET 和 EntityFramework 扩展,当我仅跟踪 1 个实体时,一切都运行良好。

现在我正在跟踪连接到第一个实体的另一个实体,当我尝试保存它时,审核保存函数会抛出反射错误

System.Reflection.TargetException: 'Object does not match target type.'

我的类(class)结构如下:

public class FirstClass{
public int ID{get;set;}
//Some props here
public SecondClass SecondClass{get;set}
}

public class SecondClass{
public int ID{get;set;}
public int FirstClassId{get;set;}
public MySpecialClass JustForData{get;set;}
//Some other props here
}

然后我只是将我的审计类建模为完全相同,但添加了审计字段

public class AuditClass{
public Guid AuditId{get;set;}
public string AuditMessage{get;set;}
}

public class FirstClassAudit : AuditClass{
public int ID{get;set;}
//Some props here
//No SecondClass prop here
}

public class SecondClassAudit: AuditClass{
public int ID{get;set;}
public int FirstClassId{get;set;}
public MySpecialClass JustForData{get;set;}
//Some other props here
}

然后在 FirstClassAudit 中省略对 SecondClass 的引用

我的两个类都在 DbContext 中,每个审计类都映射到一个单独的表。我在 AuditTypeExplicitMapper 下添加了这两个类的映射,我对其进行了调试,没有出现问题。但我仍然在 SaveChanges 函数上遇到错误

当我保存时将 SecondClass 引用保留为空时,这似乎不会发生

编辑:更多信息

Audit.NET 配置:

Audit.Core.Configuration.Setup()
.UseEntityFramework(
ef => ef
.AuditTypeExplicitMapper(m => m
.Map<FirstClass, FirstClassAudit>((frst, auditFrst) =>
{
//Map the tag fields in here
auditFrst.Tag = frst.Installation.Tag;
//Some more props here
})
.Map<SecondClass, SecondClassAudit>()
.AuditEntityAction((ev, ent, auditEntity) =>
{
((AuditClass)auditEntity).AuditMessage = ent.Action;
}))
);

DbContext 中的保存函数:

public override int SaveChanges()
{
return Helper.SaveChanges(auditContext, () => base.SaveChanges());
}

编辑2:堆栈跟踪

at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at Audit.EntityFramework.Providers.EntityFrameworkDataProvider.CreateAuditEntity(Type definingType, Type auditType, EventEntry entry) at Audit.EntityFramework.Providers.EntityFrameworkDataProvider.InsertEvent(AuditEvent auditEvent) at Audit.Core.AuditScope.SaveEvent(Boolean forceInsert) at Audit.Core.AuditScope.Save() at Audit.EntityFramework.DbContextHelper.SaveScope(IAuditDbContext context, AuditScope scope, EntityFrameworkEvent event) at Audit.EntityFramework.DbContextHelper.SaveChanges(IAuditDbContext context, Func`1 baseSaveChanges) at MyDbContext.SaveChanges() in [MyLocalPath]\MyDbContext.cs:line 132 at FirstClassRepository.UpdateFirstClass(Int32 id, FirstClassDto first) in [MyLocalPath]\FirstClassRepository.cs:line 209 at FirstClassManager.UpdateFirstClass(Int32 id, FirstClassDto dto) in [MyLocalPath]\FirstClassManager.cs:line 244 at FirstClassController.<>c__DisplayClass20_0.b__0() in [MyLocalPath]\FirstClassController.cs:line 249

编辑:经过一番摆弄后,我通过将“MySpecialClass”添加到映射来说明它是什么类型,出现错误

System.ArgumentException: 'Object of type 'MySpecialClass' cannot be converted to type 'AuditMySpecialClass'.'

这个类是 Owned Type在我的数据上下文中,这可能与它有关,也许没有。

现在,错误似乎在到达您可以在映射中添加的用户定义操作之前抛出,可能 Audit.NET 正在尝试在用户定义操作之前映射这些内容?

最佳答案

使用最新版本Audit.EntityFramework (15.0.2),您现在可以仅忽略某些审核类型的属性匹配,如下所示:

Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeExplicitMapper(m => m
.Map<FirstClass, FirstClassAudit>((frst, auditFrst) =>
{
auditFrst.Tag = frst.Installation.Tag;
})
.Map<SecondClass, SecondClassAudit>()
.AuditEntityAction((ev, ent, auditEntity) =>
{
((AuditClass)auditEntity).AuditMessage = ent.Action;
}))
.IgnoreMatchedProperties(t => t == typeof(FirstClassAudit)) // <-- Ignore prop. matching for FirstClassAudit
);

关于c# - Audit.NET 连接对象保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59752454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com