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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:37:26+00:00 2026-05-24T08:37:26+00:00

I’ve got a record I’m editing in a ASP.net web app using nhibernate and

  • 0

I’ve got a record I’m editing in a ASP.net web app using nhibernate and MVP. When I make the changes to the record, I need to check to see that one field is unique (see ‘friendlyUrl’ below). However when I so my Criteria.List call, it updates the record as well as all child records (PharmacyStoreHours) first then does the select (found this out with a SQL profile). Obviously I don’t want to update the record before the validation is complete and the validation fails as the data has been updated before the select. How do I do a select without the record updating?

As this is my first project with NHibernate, I’m not sure what would be causing this. I’ve copied what I’ve done from other parts of the code so if things look funny, please explain why they are so I can better understand what I should be doing.

NHibernate Config File Class:

  <class table="Pharmacy" name="DataAccess.Domains.Pharmacy, DataAccess">
    <id name="PharmacyId" column="PharmacyId" unsaved-value="0">
      <generator class="increment" />
    </id>
    <property name="StoreAccountNumber" column="StoreAccountNumber" type="System.String" not-null="true" />
    <property name="ClientName" column="ClientName" type="System.String" not-null="true" />
    <property name="PharmacyName" column="PharmacyName" type="System.String" not-null="true" />
    <property name="Address" column="Address" type="System.String" not-null="false" />
    <property name="AddressContinued" column="AddressContinued" type="System.String" not-null="false" />
    <property name="City" column="City" type="System.String" not-null="false" />
    <property name="State" column="State" type="System.String" not-null="false" />
    <property name="Zipcode" column="Zipcode" type="System.String" not-null="false" />
    <property name="Phone" column="Phone" type="System.String" not-null="false" />
    <property name="Fax" column="Fax" type="System.String" not-null="false" />
    <property name="Email" column="Email" type="System.String" not-null="false" />
    <property name="FriendlyUrl" column="FriendlyUrl" type="System.String" not-null="false" />
    <property name="OwnersName" column="OwnersName" type="System.String" not-null="false" />
    <property name="Latitude" column="Latitude" type="System.Decimal" not-null="false" />
    <property name="Longitude" column="Longitude" type="System.Decimal" not-null="false" />
    <bag name="Hours" table="PharmacyStoreHours" cascade="all" inverse="true">
      <key column="PharmacyId" />
      <one-to-many class="DataAccess.Domains.PharmacyWorkDays, DataAccess" />
    </bag>
  </class>
  <class table="PharmacyStoreHours" name="DataAccess.Domains.PharmacyWorkDays, DataAccess">
    <id name="PharmacyStoreHourId" column="PharmacyStoreHourId" unsaved-value="0">
      <generator class="increment" />
    </id>
    <property name="WorkDay" column="WorkDay" type="System.Int32" not-null="true" />
    <property name="OpenTime" column="OpenTime" type="System.DateTime" not-null="true" />
    <property name="CloseTime" column="CloseTime" type="System.DateTime" not-null="true" />
    <many-to-one name="PharmacyMap" class="DataAccess.Domains.Pharmacy, DataAccess" column="PharmacyId" not-null="true" />
  </class>

Code:

public bool IsFriendlyUrlUnique(string clientName, string friendlyUrl)
{
    bool result = false;

    ICriteria crit = CurrentSession.CreateCriteria(typeof (Pharmacy));
    crit.Add(new EqExpression("ClientName", clientName));
    crit.Add(new EqExpression("FriendlyUrl", friendlyUrl));
    crit.AddOrder(Order.Asc(PHARMACY_NAME));

    if (crit.List<Pharmacy>().Count == 0)
        result = true;

    return result;
}

Thanks in advance for the 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-24T08:37:27+00:00Added an answer on May 24, 2026 at 8:37 am

    Advised Solution

    I would advise, in the query, you filter out the current record.

    ICriteria crit = CurrentSession.CreateCriteria(typeof (Pharmacy));
    crit.Add(Restrictions.Not(Restrictions.Eq("PharmacyId", pharmacyIdToIgnore)));
    crit.Add(Restrictions.Eq("ClientName", clientName));
    crit.Add(Restrictions.Eq("FriendlyUrl", friendlyUrl));
    crit.AddOrder(Order.Asc(PHARMACY_NAME));
    crit.SetProjection(Projections.RowCount);
    
    return crit.UniqueResult<int> == 0;
    

    Ideally, you should perform this validation before you alter the entity itself. Also see Rune’s answer about placing a constraint in the DB to enforce this constraint. This check is not to prevent duplicates, its to try and detect them earlier so you can present a nicer error message and avoid trying to commit a change that you know will fail.

    Why this is happening (FlushMode)

    This is on purpose to ensure the results from your query are consistent with the edits you have made locally. Since NHibernate is trying to model your entities as if they are always available in memory by automatically persisting to/from the database as required.

    You can change this by looking at the auto flush setting. There is a default value on the session factory used for any newly created sessions, and an individual setting on each session.

    The default value is FlushMode.Auto which will save when a transaction is committed or before running any query that might be affected by changes in memory. If you change it to FlushMode.Commit then it will only persist changes back to the database when committing a transaction, or when saving a new entity that uses a primary key strategy such as identity.

    You can also manually persist the changes in the current session back to the database at any time using Session.Flush.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
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
I need to clean up various Word 'smart' characters in user input, including but

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.