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

  • Home
  • SEARCH
  • 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 3497988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:26:21+00:00 2026-05-18T12:26:21+00:00

My application has the following entities: public class User { public virtual int UserID

  • 0

My application has the following entities:

public class User
{
    public virtual int UserID { get; set; }
    public virtual string UserName { get; set; }
    public virtual IList<UserLog> Log { get; private set; }

    public User()
    {
        Log = new List<UserLog>();
    }
}

public class UserLog
{
    public virtual int UserLogID { get; set; }
    public virtual User User { get; set; }
    public virtual string UserName { get; set; }
    public virtual DateTime DateCreated { get; set; }
}

With the following fluent mappings:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("Users");
        Id(x => x.UserID);
        Map(x => x.UserName);
        HasMany(x => x.Log)
            .KeyColumn("UserID")
            .OrderBy("DateCreated")
            .Inverse()
            .Cascade.All();
    }
}

public class UserLogMap : ClassMap<UserLog>
{
    public UserLogMap()
    {
        Table("UsersLog");
        Id(x => x.UserLogID);
        References(x => x.User, "UserID");
        Map(x => x.UserName);
        Map(x => x.DateCreated);
    }
}

The UsersLog table simply logs any changes which are made to the User. I’m trying to hook this up automatically using the NHibernate event listeners. I have setup my configuration which successfully calls the following 2 methods:

public void OnPostInsert(PostInsertEvent @event)
{
    if (@event.Entity is User)
        InsertLog(@event.Entity);
}

public void OnPostUpdate(PostUpdateEvent @event)
{
    if (@event.Entity is User)
        InsertLog(@event.Entity);
}

Edit (here is the InsertLog method):

private void InsertLog(object entity)
{
    var context = ServiceLocator.Current.GetInstance<IDataContext>();
    var user = (User)entity;
    context.Repository<UserLog>().Insert(new UserLog
    {
        User = user,
        UserName = user.UserName,
        DateCreated = DateTime.UtcNow
    }); // Insert calls ISession.SaveOrUpdate(entity)
}

When i update a record a log is successfully inserted but when i insert a record i get an error telling me the UserID cannot be null in the UsersLog table. I’ve played around with a few different variations but nothing seems to work.

I’d really appreciate it if someone could show me how this can be done. Thanks

  • 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-05-18T12:26:22+00:00Added an answer on May 18, 2026 at 12:26 pm

    Solution posted in question comments. I simply had to use:

    @event.Session.GetSession(NHibernate.EntityMode.Poco).Save(...);
    

    to save the user instead of accessing the repository.

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

Sidebar

Related Questions

my application has the following entity: public class User { public virtual int UserID
my application has the following structure: public class Transaction { public int TransactionID {
Say, I am building a business process management application. It has the following entities:
My application has a need to let the user choose a date from a
I want to achieve the following in Linq to Entities: Get all Enquires that
My application has the following components: 2 Activities A Service and a BroadcastReceiver Whenever
my application has the following database structure: Transactions: - TransactionID (PK, Identity) - Type
My application has the following UI configuration: The main form is an MDI container.
I have an application architecture which has following layers (or c# projects). web front
My application has a following requirement. 1) First screen has two tabs. 2) From

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.