Sterno
Sterno

Reputation: 1658

How do I get the AuditTable attribute to work?

I'm trying to set up Nhibernate Envers for my ASP.NET web app. I'm using Fluent Nhibernate and have an entity like the one below. I'd like Envers to use the table audUsers for auditing, but it keeps insisting on using tblUsers_AUD.

Entity

[AuditTable("audUsers")]
public class User
{
   public virtual string Name {get; set;}
   public virtual string EmailAddress {get; set;}
}

Mapping

public class UserMapping : ClassMap<User>
{
    public UserMapping()
    {
        Map(m => m.Name).Not.Nullable();
        Map(m => m.EmailAddress).Not.Nullable();
    }
}

Nhibernate Configuration

public static ISessionFactory BuildSessionFactory<T>() where T : ICurrentSessionContext
{
    var connectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
    return Fluently
        .Configure()
        .Database(OracleDataClientConfiguration.Oracle10.ConnectionString(connectionString))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateConfiguration>()
            .Conventions.AddFromAssemblyOf<TableNameConvention>())
        .CurrentSessionContext<T>()
        .ExposeConfiguration(ConfigureEnvers)
        .BuildSessionFactory();
}

private static void ConfigureEnvers(NHibernate.Cfg.Configuration nhConf)
{
    var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
    enversConf.Audit<User>();
    nhConf.IntegrateWithEnvers(enversConf);
}

And, just in case the nHibernate convention I have set up matters:

Nhibernate Conventions

public class TableNameConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        instance.Table(string.Format("tbl{0}s", instance.EntityType.Name));
    }
}

The problem I'm having is that the AuditTable attribute on the User entity doesn't actually seem to do anything. Envers is still just using the default audit table naming of sticking and _aud suffix on the end. I've been able to successfully configure it to change the prefix or suffix, but I'd really rather change the name entirely, mostly due to the table name character limit imposed by Oracle. Otherwise, I'll run into problems with table names that are right up against the character limit being too long when a prefix or suffix is attached.

Upvotes: 2

Views: 1453

Answers (1)

Roger
Roger

Reputation: 1950

NHibernate.Envers supports two kinds of configurations, either using attributes or configured in code - you cannot mix these. In your nhCfg.IntegrateWithEnvers you pass in your Envers configuration. This object could be a FluentConfiguration or AttributeConfiguration object.

You're using FluentConfiguration, that means that your configuration is made in code on this very object - Envers attributes are ignored.

The counter part to AuditTable attribute for FluentConfiguration is

var enversCfg = new FluentConfiguration();
enversCfg.Audit<User>()
   .SetTableInfo(table => table.Value = "audUsers");

Upvotes: 2

Related Questions