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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:24:27+00:00 2026-06-07T14:24:27+00:00

I would like to approach database versioning and automated upgrades in NHibernate from a

  • 0

I would like to approach database versioning and automated upgrades in NHibernate from a different direction than most of the strategies proposed out there.

As each object is defined by an XML mapping, I would like to take size and checksum for each mapping file/ configuration and store that in a document database (raven or something) along with a potential custom update script. If no script is found, use the NHibernate DDL generator to update the object schema. This way I can detect changes, and if I need to make DML changes in addition to DDL, or perform a carefully ordered transformation, I can theoretically do so in a controlled, testable manner. This should also maintain a certain level of persistence-layer agnosticism, although I’d imagine the scripts would still necessarily be database system-specific.

The trick would be, generating the “old” mapping files from the database and comparing them to the current mapping files. I don’t know if this is possible. I also don’t know if I’m missing anything else that would make this strategy prohibitively impractical.

My question, then: how practical is this strategy, and why?

  • 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-07T14:24:28+00:00Added an answer on June 7, 2026 at 2:24 pm

    what i did to solve just that problem

    1. version the database in a table called SchemaVersion
    2. query the table to see if schema is up to date (required version stored in DAL), if yes goto 6.
    3. get updatescript with version == versionFromBb from resources/webservices/…
    4. run the script which also alters the schemaversion to the new version
    5. goto 2.
    6. run app

    to generate the scripts i have used 2 options

    1. support one rdbms: run SchemaUpdate to export into file and add DML statements manually
    2. support multiple rdbms: use Nhibernate class Table to generate at runtime ddl to add/alter/delete tables and code which uses a session DML

    Update:

    "what method did you use to store the current version"

    small example

    something like this

    public static class Constants
    {
        public static readonly Version DatabaseSchemaVersion = new Version(1, 2, 3, 4);
    }
    
    public class DBMigration
    {
        private IDictionary<Version, Action> _updates = new Dictionary<Version, Action>();
        private Configuration _config;
        private Dialect _dialect;
        private IList<Action<ISession>> _actions = new List<Action<ISession>>(16);
        private string _defaultCatalog;
        private string _defaultSchema;
    
        private void CreateTable(string name, Action<Table> configuretable)
        {
            var table = new Table(name);
            configuretable(table);
    
            string createTable = table.SqlCreateString(_dialect, _config.BuildMapping(), _defaultCatalog, _defaultSchema);
            _actions.Add(session => session.CreateSQLQuery(createTable).ExecuteUpdate());
        }
    
        private void UpdateVersionTo(Version version)
        {
            _actions.Add(session => { session.Get<SchemaVersion>(1).Value = version; session.Flush(); });
        }
    
        private void WithSession(Action<session> action)
        {
            _actions.Add(action);
        }
    
        public void Execute(Configuration config)
        {
            _actions.Clear();
            _defaultCatalog = config.Properties[NH.Environment.DefaultCatalog];
            _defaultSchema = config.Properties[NH.Environment.DefaultSchema];
            _config = config;
            _dialect = Dialect.GetDialect(config.Properties);
    
            using (var sf = _config.BuildSessionFactory())
            using (var session = sf.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                Version dbVersion = session.Get<SchemaVersion>(1).Value;
                while (dbVersion < Constants.DatabaseSchemaVersion)
                {
                    _actions.Clear();
                    _updates[dbVersion].Invoke(); // init migration, TODO: error handling
                    foreach (var action in _actions)
                    {
                        action.Invoke(session);
                    }
                    tx.Commit();
                    session.Clear();
                    dbVersion = session.Get<SchemaVersion>(1).Value;
                }
            }
        }
    
        public DBMigration()
        {
            _updates.Add(new Version(1, 0, 0, 0), UpdateFromVersion1);
            _updates.Add(new Version(1, 0, 1, 0), UpdateFromVersion1);
            ...
        }
    
        private void UpdateFromVersion1()
        {
            AddTable("Users", table => table.AddColumn(...));
            WithSession(session => session.CreateSqlQuery("INSERT INTO ..."));
            UpdateVersionTo(new Version(1,0,1,0));
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate an entity-relationship diagram (ERD) from an existing PostgreSQL database.
I would like to find out if my database is running or not from
I am retrieving tables and their respective fields from a database, and would like
I would like to dynamically generate a form from a database in ASP.NET, what
I would like to use Entity Framework Code first approach with SQLCE4 database. Everything
I would like some advice on the best approach to use in the following
I would like to convert this fluent approach to xml: container.Register( AllTypes.FromAssemblyNamed(Company.DataAccess) .BasedOn(typeof(IReadDao<>)).WithService.FromInterface(), AllTypes.FromAssemblyNamed(Framework.DataAccess.NHibernateProvider)
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like to be able to set colors of headings and such, different font
I have a database process written in PL/SQL that i would like to test

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.