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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:52:23+00:00 2026-05-13T22:52:23+00:00

We are trying to implement the Microsoft Sync Framework into our application that persists

  • 0

We are trying to implement the Microsoft Sync Framework into our application that persists it’s domain using NHibernate.

One of the problems we encountered is that after the Sync Framework has altered your initial database structure (adding shadow tables and triggers) NHibernate seems to get upset by throwing an toomanyrowsaffectedexception when you try to insert objects into the database.

I found this article that has the solution of adding SET NOCOUNT ON and OFF around each update statement, but since the table structure is automatically generated by nhibernate and the sync triggers are automatically generated by the Sync Framework adjusting all triggers manually is not really an option.

http://www.codewrecks.com/blog/index.php/2009/03/25/nhibernate-and-toomanyrowsaffectedexception/

I tried setting the sql server 2008 property NOCOUNT on as described in this question: Where's the best place to SET NOCOUNT?
but this resulted in a StaleStateException (-1 rows affected, expected 1).

Do you guys know if there is a way to configure the sync framework to automatically set these NOCOUNT statements in it’s triggers? Or maybe is there a way to tell NHibernate to expect more/less rows to have been changed?
Or maybe any of you have an automated script to add these NOCOUNT statements to the sync framework’s triggers.

Thx in advance!

  • 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-13T22:52:23+00:00Added an answer on May 13, 2026 at 10:52 pm

    I think the NOCOUNT way is the way to go. You could do this by setting the NOCOUNT for all tables used by the sync framework. See the code below. Another way is to patch NHibernate and ignore the updatecount see (https://nhibernate.jira.com/browse/NH-1353).

    KR,

    Paul

        class SqlSyncTriggerHelper
    {
        private const string triggerSql = @"select sys.triggers.name from sys.triggers, sys.objects
            where sys.objects.name='{0}' and sys.objects.type = 'U' and sys.triggers.parent_id = sys.objects.object_id";
    
        private DbSyncScopeDescription syncScopeDescription;
    
        public SqlSyncTriggerHelper(DbSyncScopeDescription syncScopeDescription)
        {
            this.syncScopeDescription = syncScopeDescription;
        }
    
        public void Apply(SqlConnection conn)
        {
            SqlTransaction transaction = null;
            try
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                transaction = conn.BeginTransaction();
                foreach (var table in syncScopeDescription.Tables)
                {
                    foreach (string trigger in GetTriggers(table.UnquotedLocalName, conn, transaction))
                    {
                        AlterTrigger(trigger, conn, transaction);
                    }
                }
                transaction.Commit();
            }
            catch
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                throw;
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                conn.Close();
            }
        }
    
        private void AlterTrigger(string trigger, SqlConnection conn, SqlTransaction transaction)
        {
            SqlCommand newCmd = new SqlCommand(string.Format("exec sp_helptext '{0}'", trigger), conn, transaction);
            var triggerStringBuilder = new StringBuilder();
            using (var reader = newCmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    triggerStringBuilder.Append(reader.GetValue(0) as string);
                }
            }
            var triggerString = triggerStringBuilder.ToString();
            triggerString = triggerString.Replace("CREATE TRIGGER", "ALTER TRIGGER").Replace(" AS\n", " AS\nSET NOCOUNT ON\n") + "\nSET NOCOUNT OFF";
            var alterTriggerCommand = new SqlCommand(triggerString, conn, transaction);
            alterTriggerCommand.ExecuteNonQuery();
        }
    
        private IEnumerable<string> GetTriggers(string tableName, SqlConnection conn, SqlTransaction transaction)
        {
            var resultList = new List<string>();
            var command = new SqlCommand(string.Format(triggerSql, tableName), conn, transaction);
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    resultList.Add(reader.GetString(0));
                }
            }
            return resultList;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Depending on what you're asking for, these are either "Options… May 14, 2026 at 5:12 am
  • Editorial Team
    Editorial Team added an answer Based on the lack of answers it sounds like nhocr… May 14, 2026 at 5:12 am
  • Editorial Team
    Editorial Team added an answer There is (git rebase --onto), but in this case you… May 14, 2026 at 5:12 am

Related Questions

I'm wondering how many folks using the Microsoft development stack (IIS and/or ASP.NET) are
I'm evaluating Microsoft Team Foundation Server for my customer, who currently uses Visual SourceSafe
http://en.wikipedia.org/wiki/ICalendar I'm working to implement an export feature for events. The link above lists
I need to implement a feature similar to the one provided by Microsoft Outlook
I need to implement a custom handler for MVC that gives me the first

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.