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 8544771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:25+00:00 2026-06-11T12:38:25+00:00

I’m using fluent nhibernate for the first time to phase out my existing database

  • 0

I’m using fluent nhibernate for the first time to phase out my existing database access layer for accessing a remote MySql server.

The clients are mostly virtualized and can go to sleep/hibernate at any point in time, for any length of time (seconds to days). Even though they are usually time synchronous with the server that cannot be guaranteed, which means I cannot use the client provided DateTime for some fields.

How do I get nhibernate to set/update certain fields with the server time (preferably by calling the UTC_TIMESTAMP() function)?

Object:

public class MyObject{
  public virtual UInt64 Id { get; set; }
  public virtual String Status{ get; set; }
  public virtual String SomeData{ get; set; }

  //Set only once when the Object is inserted
  public virtual DateTime TimeCreated { get; set; }
  //Set every time the object is updated
  public virtual DateTime TimeLastUpdate { get; set; }
  //Set every time the 'Status' column is updated
  public virtual DateTime TimeStatusLastChanged { get; set; }
  //This is a user provides standard datetime field
  public virtual DateTime SomeUserSpecifiedTime { get; set; }
}

Mapping:

Id(x => x.Id)
  .GeneratedBy.Native();
Map(x => x.Status);
Map(x => x.SomeData);
Map(x => x.SomeUserSpecifiedTime);
//? -->
Map(x => x.TimeCreated)
  .Not.Update();
Map(x => x.TimeLastUpdate);
Map(x => x.TimeStatusLastChanged);
//<-- ?

I want the three Time*-fields to use the UTC_TIMESTAMP() function, when they are set/updated, so the server side date/time gets inserted. The DateTime SomeUserSpecifiedTime field is a standard mapped field.

Until now my database access class contained the logic to create the queries in that way. I could create database triggers with the .Generated.Insert/Always mapping, but I was hoping there is a nhibernate/code-only way of doing that.

One solution I have found, is fetching the time from the server and providing it for the insert/update, but that is out, because the time between fetch and insert/update can be significant.

  • 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-11T12:38:26+00:00Added an answer on June 11, 2026 at 12:38 pm

    I’ve decided to solve the problem using triggers, which is not optimal, but works for now. Unfortunately nhibernate does not have a concept of triggers, so you have to declare them as auxiliary database objects, which leads to some problems

    • SchemaValidator and SchemaUpdate will not pick up on auxDbObjects and thus will not update/create them. SchemaExport will create them, but will do so twice which leads to even more code duplication.
    • Need to create and register an instance for each trigger, which is cumbersome.

    It might look something like this

    public class MyTrigger : IAuxiliaryDatabaseObject{
    
      public string SqlCreateString(Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema){
        //The drop is important, because the shema export calls and executes this twice.
        return @"
    DROP TRIGGER IF EXISTS myTrigger;
    
    CREATE TRIGGER myTrigger BEFORE INSERT ON myObjectTable FOR EACH ROW 
    BEGIN
    set new.TimeAdded = UTC_TIMESTAMP();
    END";
        }
    
        public string SqlDropString(Dialect dialect, string defaultCatalog, string defaultSchema){
        return @"DROP TRIGGER IF EXISTS myTrigger";
        }
    
      public void AddDialectScope(string dialectName){
        throw new NotImplementedException();
        }
    
      public bool AppliesToDialect(Dialect dialect){
        return true;
        }
    
      public void SetParameterValues(IDictionary<string, string> parameters){
        throw new NotImplementedException();
        }
      }
    

    In the configuration each trigger/aux. database object must be added individually:

    Fluently.Configure()
      .Database(...)
      .ExposeConfiguration(conf =>
        {
        conf.AddAuxiliaryDatabaseObject(new MyTrigger());
        });
    

    The mapping

    Map(x => x.TimeCreated)
      .Not.Update()
      .CustomType("UtcDateTime")
      .ReadOnly()
      .Generated.Insert()   //.Generated.Always() for columns that track lastModification
      ;
    

    Custom user types or interceptors do not work unfortunately so it seems this is the only possible way to achieve this at the moment.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.