这是我的第一种任务,我正在使用本教程。 Link
不同之处在于本教程是在单个层中进行的,因此可以轻松访问Identity属性。
我的项目有
问题是我将Audit模型放在实体层中,但是在教程中它具有AspNetUsers表的外键,并且代码优先迁移没有通过。
he ForeignKeyAttribute on property 'AuditUserId' on type 'Namespace.Entities.AuditActions.Audit' is not valid. The navigation property 'AuditUser' was not found on the dependent type 'Namespace.Entities.AuditActions.Audit'. The Name value should be a valid navigation property name
第二个问题是我无法从数据层访问用户信息,我已经在其中放置了保存覆盖。
var currentUser = OwinContextHelper.CurrentApplicationUser;
我无法访问OwinHelpper类,它位于放置覆盖的数据层的表示层中。
如何在我的应用程序中实现本教程?
任何想法都会受到欢迎。
对我来说很容易是一个初级开发者:)
试图帮助解决您的第二个问题:
我会推荐Audit.NET / Audit.EF库用于这种用例(实际上我猜你已经在使用它了)。
您可以使用Custom Action避免从表示层传递到数据层的信息。该库使您可以挂钩审计创建,并且可以将该代码放在表示层上,例如:
class Presentation
{
void StartUp()
{
// Use SqlServerDataProvider to store the audit events on SQL
Audit.Core.Configuration.Setup()
.UseSqlServer(_ => _
.ConnectionString("...")
.TableName("Event")
.IdColumnName("EventId")
.JsonColumnName("Data"));
// Add a custom action to have a custom field
Audit.Core.Configuration.AddOnCreatedAction(scope =>
{
scope.SetCustomField("OwinUser", OwinContextHelper.CurrentApplicationUser);
});
}
}