数据库第一种自动CRUD日志记录方法

时间:2018-05-25 23:00:02

标签: entity-framework-6 audit entity-framework-plus

我已按如下方式设置EF + Audit:

public partial class FocusConnection : DbContext
{
    static FocusConnection()
    {
        AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
           // ADD "Where(x => x.AuditEntryID == 0)" to allow multiple SaveChanges with same Audit
           (context as FocusConnection).AuditEntries.AddRange(audit.Entries);
    }

    public override int SaveChanges()
    {
        var audit = new Audit();
        audit.PreSaveChanges(this);
        var rowAffecteds = base.SaveChanges();
        audit.PostSaveChanges();

        if (audit.Configuration.AutoSavePreAction != null)
        {
            audit.Configuration.AutoSavePreAction(this, audit);
            base.SaveChanges();
        }

        return rowAffecteds;
    }

    public override Task<int> SaveChangesAsync()
    {
        return SaveChangesAsync(CancellationToken.None);
    }

    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken)
    {
        var audit = new Audit();
        audit.PreSaveChanges(this);
        var rowAffecteds = await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        audit.PostSaveChanges();

        if (audit.Configuration.AutoSavePreAction != null)
        {
            audit.Configuration.AutoSavePreAction(this, audit);
            await base.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        }

        return rowAffecteds;
    }

}

我使用了一个部分类来保留由EF6生成的文本模板化原始上下文。我使用以下SQL脚本按原样生成两个表:

CREATE TABLE [dbo].[AuditEntries] (
    [AuditEntryID] [int] NOT NULL IDENTITY,
    [EntitySetName] [nvarchar](255),
    [EntityTypeName] [nvarchar](255),
    [State] [int] NOT NULL,
    [StateName] [nvarchar](255),
    [CreatedBy] [nvarchar](255),
    [CreatedDate] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.AuditEntries] PRIMARY KEY ([AuditEntryID])
)

GO

CREATE TABLE [dbo].[AuditEntryProperties] (
    [AuditEntryPropertyID] [int] NOT NULL IDENTITY,
    [AuditEntryID] [int] NOT NULL,
    [RelationName] [nvarchar](255),
    [PropertyName] [nvarchar](255),
    [OldValue] [nvarchar](max),
    [NewValue] [nvarchar](max),
    CONSTRAINT [PK_dbo.AuditEntryProperties] PRIMARY KEY ([AuditEntryPropertyID])
)

GO

CREATE INDEX [IX_AuditEntryID] ON [dbo].[AuditEntryProperties]([AuditEntryID])

GO

ALTER TABLE [dbo].[AuditEntryProperties] 
ADD CONSTRAINT [FK_dbo.AuditEntryProperties_dbo.AuditEntries_AuditEntryID] 
FOREIGN KEY ([AuditEntryID])
REFERENCES [dbo].[AuditEntries] ([AuditEntryID])
ON DELETE CASCADE

GO

我在静态构造函数中遇到错误:

static FocusConnection()
{
    AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
        // ADD "Where(x => x.AuditEntryID == 0)" to allow multiple SaveChanges with same Audit
        (context as FocusConnection).AuditEntries.AddRange(audit.Entries);
}

AuditEntries.AddRange(audit.Entries)上的错误: CS1503参数1:无法从System.Collections.Generic.List<Z.EntityFramework.Plus.AuditEntry>转换为System.Collections.Generic.IEnumerable<DA.Systems.Focus.Data.EntityFramework.FocusOrm.AuditEntry>

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

您使用数据库优先。

EF +中的 $(window).scroll(function(){ if ($(this).scrollTop() > 80) { $('.navbar').removeClass('.navbar').addClass("navbar-alt").fadeIn(500); $('.logo-ate').removeClass('.logo-ate').addClass("logo-ate-alt"); $('.links').removeClass('.links').addClass("links-alt"); $('.menu').removeClass('.menu').addClass("menu-alt"); } else { $('.navbar').removeClass("navbar-alt").addClass('.navbar').fadeOut(500); $('.logo-ate').removeClass("logo-ate-alt").addClass('.logo-ate'); $('.links').removeClass("links-alt").addClass('.links'); $('.menu').removeClass("menu-alt").addClass('.menu'); } }); }); 类与您的上下文生成的类不同。

您需要AuditEntry值。

Import