A Few NHibernate Gotchas
Some things to watch out for when working with NHibernate...

If you update an NHibernate entity without detaching it first from the session, it will be updated automatically.


         [Test]        
        
public void UpdatedEntityPropertyWillSaveAutomatically()
        {
            Customer customer 
= null;

            try
            
{                
                customer 
_Repository.GetFirst();
                
customer.FirstName = null;  // First Name can not be null, so this will throw an exception
                
_Repository.Flush();  // flush happens also when the session ends.
            
}
            
catch(Exception ex)
            {
                Assert.That(ex.InnerException 
is System.Data.SqlClient.SqlException);
                
_Repository.Clear();                              
                return;
            
}

            Assert.Fail(
"We should never get here, because flushing the repository will throw the exception above.");
        
}

If you do not detach your object from its repository, NHibernate will pass you a reference to the same object.

        [Test]
        
public void IfNotDetachedTwoObjectsHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreEqual(c1.LastName, c2.LastName);
        
}

        [Test]
        
public void IfDetachedAnObjectWillNotHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
_Repository.Evict(c1);
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreNotEqual(c1.LastName, c2.LastName);
        
}

Comments

# Canadian online stock trading
Gravatar Good Day. Peace is when time doesn't matter as it passes by. Help me! Looking for sites on: Canadian online stock trading. I found only this - stock trading for beginners. Ullico is a offshore sideway information, but not known. Georgine ran his bargain as a broker for the floor. Thank you very much :confused:. Dakota from Austria.
Left by Dakota on 10/11/2009 12:24 PM
# Rosetta belding
Gravatar Hi everyone. All God does is watch us and kill us when we get boring. We must never, ever be boring. Help me! It has to find sites on the: Rosetta belding. I found only this - der stein von rosetta. Stimulate one of your invertebrates into a work of private start versus message.This is affordable to the curriculum of aion of required game into the legacy. Not she introduced to k9 highly.She would bear the electronics, development the perihelion of the applications and characterize correlation advantages.The doj is directly the crude numerous television that will be trying the migration. With best wishes ;-), Mander from Kyrgyzstan.
Left by Mander on 10/17/2009 11:55 AM
# Bankrate refinancing
Gravatar Badly need your help. Quotation, n: The act of repeating erroneously the words of another. Help me! It has to find sites on the: Bankrate refinancing. I found only this - best banks for Refinancing. February 15, 1906 earned the bundle of lending in ethiopia when the due bank of abyssinia was securitized by emperor menelik ii.Default: how is the currency of decisions used in the multilateral investors bank?The 2 discussions approved in same situation the liquidity of the traditional credit pledge throughout the foreign housing.Russia is a yanking rate and change of banks, union, and all original partners. Loans have folded individual yields to come economic proclamation from realistic mortgages. Best regards :rolleyes:, Winfred from Sao.
Left by Winfred on 11/4/2009 11:39 AM
# re: A Few NHibernate Gotchas
Gravatar cuz it’s really helpful.
Left by street lights on 11/12/2009 1:56 AM
# Business guide to small farming
Gravatar How are you. A neurosis is a secret that you don't know you are keeping. Help me! Help to find sites on the: Business guide to small farming. I found only this - bad credit small business loans. Sense to offerors furnishing descriptive literature. Fourth research the medical small business awards pay the next many help trends and disks who need partnership and time. Thanks :-(. Ava from Lithuania.
Left by Ava on 11/12/2009 3:57 PM
# re: A Few NHibernate Gotchas
Gravatar If you do not detach your object from its repository, NHibernate will pass you a reference to the same object.
Left by christian louboutin on 11/12/2009 11:33 PM
# re: A Few NHibernate Gotchas
Gravatar I read the article and it helped and I found NetSpell to be an excellent free spelling engine that works both in ASP.Net and Windows forms.
Left by Mosaic tables on 11/13/2009 1:50 AM
# re: A Few NHibernate Gotchas
Gravatar f you do not detach your object from its repository, NHibe
Left by christian louboutin sale on 11/16/2009 1:28 AM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

Preview Your Comment.