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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:04:27+00:00 2026-05-15T08:04:27+00:00

I am inserting a record into my table, but I want to be able

  • 0

I am inserting a record into my table, but I want to be able to set my
SortOrder field on insert to SELECT MAX(SortOrder) + 1 FROM Category
WHERE SiteID = @SiteID. What is the easiest way of doing this?

Here is my data structure:
Category
ID
SiteID
SortOrder
Name

I am using Fluent NHibernate and Linq to NHibernate. Thanks for any 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-05-15T08:04:28+00:00Added an answer on May 15, 2026 at 8:04 am

    Ok, here are 2 ways. The simplest may not involve NHibernate since you’re using SQL Server… You could just create an INSTEAD OF INSERT trigger on your table, like so:

    CREATE TRIGGER tgINSCategory ON Category INSTEAD OF INSERT
    AS 
    BEGIN
        SET NOCOUNT ON
    
        INSERT INTO Category (SiteID, Name, SortOrder)
        SELECT SiteID, Name, 
            ISNULL((SELECT MAX(c.SortOrder)
                    FROM Category c INNER JOIN INSERTED i ON c.SiteID = i.SiteID), 0) + 1   
        FROM INSERTED
    END
    

    In this first scenario you would just NHibernate map the SortOrder column as read only (just add a .ReadOnly() on the fluent nhibernate class map SortOrder property).

    The second way would be to use NHibernate’s built in feature called interceptors. With an interceptor you can run whatever code you like just at the time when NHibernate would normally generate SQL to execute a DML action. The below auto-inserts creation info but would be very similar to what you would need. IInsertLoggable is a custom interface that simply lists the properties/columns of interest for interception.

    using System;
    using System.Reflection;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate;
    
        public class InsertDefaults : EmptyInterceptor {
            private const string CREATED_BY = "CreatedById";
    
            private Hashtable GetInsertLoggablePropertyIndexes(string[] Properties) {
                Hashtable result = new Hashtable();
                for (int i = 0; i < Properties.Length; i++) {
                    if (Properties[i] == CREATED_BY) {
                        result.Add(CREATED_BY, i);
                        break;
                    } 
                }
                return result;
            }
    
            public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types) {
                if (entity is IInsertLoggable) {  
                    Hashtable indexes = GetInsertLoggablePropertyIndexes(propertyNames);
                    state[(int)indexes[CREATED_BY]] = currentUser;
                    PropertyInfo createdByProp = entity.GetType().GetProperty(CREATED_BY);
                    if (createdByProp != null)
                        createdByProp.SetValue(entity, currentUser, null);
                }
                return base.OnSave(entity, id, state, propertyNames, types);
            }
        }
    

    In my opinion this sort of classic trigger operation should probably live on the database and I’d go with the first option…

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

Sidebar

Related Questions

No related questions found

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.