Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:32:23+00:00 2026-06-05T07:32:23+00:00

I need some help setting up my Fluent NHibernate POCO class structure. I’m trying

  • 0

I need some help setting up my Fluent NHibernate POCO class structure. I’m trying to simply have a way to make an object auditable. On Create, I want my created and modified datetimes to be set and on update, I want my modified to be updated. I’ve been following some examples, but I have hit a road block. This is my current setup:

IAuditable.cs

namespace ZeroBase.Domain.Entities
{
public interface IAuditable
{
    DateTime Created
    {
        get;
    }

    DateTime Modified
    {
        get;
    }

    string CreatedPropertyName
    {
        get;
    }

    string ModifiedPropertyName
    {
        get;
    }

    void SetCreationDate(DateTime created);
    void SetModifiedDate(DateTime modified);
}
}

AuditableEntity.cs

namespace ZeroBase.Domain.Entities
{
public class AuditableEntity<T> : IAuditable
{
    public DateTime Created { get; private set; }
    public DateTime Modified { get; private set; }

    void IAuditable.SetCreationDate(DateTime created)
    {
        this.Created = created;
    }

    void IAuditable.SetModifiedDate(DateTime modified)
    {
        this.Modified = modified;
    }

    string IAuditable.CreatedPropertyName
    {
        get
        {
            string createdPropName = "Created";

        #if DEBUG
            CheckIfPropertyExists(createdPropName);
        #endif

            return createdPropName;
        }
    }

    string IAuditable.ModifiedPropertyName
    {
        get
        {
            string modifiedPropName = "Modified";

        #if DEBUG
            CheckIfPropertyExists(modifiedPropName);
        #endif

            return modifiedPropName;
        }
    }

    private void CheckIfPropertyExists(string propertyName)
    {
        PropertyInfo pi = this.GetType().GetProperty(propertyName);
        Debug.Assert(pi != null, String.Format("There exists no property {0}", propertyName));
    }
}
}

User.cs

namespace ZeroBase.Domain.Entities
{
public class User : AuditableEntity<User>
{
    public virtual Guid Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string EmailAddress { get; set; }
    public virtual IEnumerable<Comment> Comments { get; set; }
}
}

AuditInterceptor.cs

namespace ZeroBase.Infrastructure.Data
{
public class AuditInterceptor : EmptyInterceptor
{
    public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState,
        string[] propertyNames, NHibernate.Type.IType[] types)
    {
        IAuditable auditableObject = entity as IAuditable;

        if (auditableObject != null)
        {
            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (propertyNames[i] == auditableObject.ModifiedPropertyName)
                {
                    currentState[i] = DateTime.Now;
                }
            }
            return true;
        }

        return false;
    }

    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
    {
        IAuditable auditableObject = entity as IAuditable;

        if (auditableObject != null)
        {
            DateTime currentDate = DateTime.Now;

            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (propertyNames[i] == auditableObject.CreatedPropertyName)
                {
                    state[i] = currentDate;
                }
                if (propertyNames[i] == auditableObject.ModifiedPropertyName)
                {
                    state[i] = currentDate;
                }
            }

            System.Diagnostics.Debug.WriteLine("interceptor: created: " + auditableObject.Created);
            System.Diagnostics.Debug.WriteLine("interceptor: modified: " + auditableObject.Modified);

            return true;
        }

        return false;
    }
}
}

AuditMap.cs

namespace ZeroBase.Infrastructure.Data
{
public class AuditMap<T>: ClassMap<T> where T : AuditableEntity<T>
{
    public AuditMap()
    {
        Map(p => p.Created);
        Map(p => p.Modified);
    }
}
}

UserMap.cs

namespace ZeroBase.Infrastructure.Data
{
public class UserMap : AuditMap<User>
{
    public UserMap()
    {
        Id(x => x.Id)
            .Column("Id")
            .GeneratedBy.Guid();
        Map(x => x.Username);
        Map(x => x.Password);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.EmailAddress);
        HasMany(x => x.Comments);
        Table("Users");
    }
}
}

SessionHelper.cs

_sessionFactory = Fluently.Configure()

            // Set up database connection
            .Database(MsSqlConfiguration.MsSql2005
                .ConnectionString(x => x.Is(_connectionString))
                //.ShowSql()
            )

            // Use class mappings
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())

            .ExposeConfiguration(c => c.SetInterceptor(new AuditInterceptor()))

            .BuildSessionFactory();

When I try to run this, I get this runtime error:
The following types may not be used as proxies:
ZeroBase.Domain.Entities.User: method get_Created should be ‘public/protected virtual’ or ‘protected internal virtual’
ZeroBase.Domain.Entities.User: method get_Modified should be ‘public/protected virtual’ or ‘protected internal virtual'”}

What is this trying to tell me?

Does this have anything to do with Fluent NHibernate?

I’m a little confused and would love some help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:32:24+00:00Added an answer on June 5, 2026 at 7:32 am

    Created and Modified properties in your AuditableEntity<T> class need to be virtual. This is a requirement of NHibernate, if you are using lazy loading (on by default).

    Also, there are some questions about it here on SO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help - I have tried setting setSpecificDate so that the array
I need some help setting up a particular terrain. I have a world that
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help with a query.. I have three tables. Source id name 1
Need some help with DataFormatString in GridView. I have a Double value that needs
I need some help about WCF and authorization. Currently I have a client which
As the title says, I need some help setting up my ViewModelLocator. It's a
I need some help setting up a PHP array. I get a little lost

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.