SQL Server Uniqueindentifier

Topic updated 29-10-2007

The uniqueindentifier datatype is a special type which allows you to identify a row by using a (globally) unique GUID.

You can read this article for more details: http://www.sqlteam.com/article/uniqueidentifier-vs-identity

 

Normally, you will configure the MsSQL Column as :

RowGuid = Yes
Default Value (or binding)  = Newguid()

 

If this is your case it means that it should be treated as an auto-numbered column.

 

GenWise can't determine this automatically so you will need to manually set the AutoNumber=Server as shown in image below:

MsSQLUniqueIndentifier_column

 

XML Mapping (special generator)

When setting correctly the autonumber property for the column you will notice that the XML mapping will change to use a special NHibernate ID generator : guid.

 

<class name="Contract"

  table="[dbo].[Contracts]">

   <id name="ContractID"

    access="nosetter.camelcase-underscore"

    column="`ContractID`"

    type="System.Guid"   >

     <generator class="guid" />

   </id>

 

Example Unit Test

to prove the behaviour here is a small unit test that inserts a new object without assigning the PK, the generator (guid) will set a value after the insert is done.

   [Test,Explicit]

               public void InsertTest()

               {

           ClubFactory factory = new ClubFactory(SessionScope.PerOperation);

 

           int countriesCount = factory.GetAll().Count;

           Assert.Greater(countriesCount, 1);

                       

           Club club = new Club();

               club.Description = "Sebastian";

 

           // Test that the default value before the insert is null.

           Assert.AreEqual(club.ClubID , Guid.Empty);

           factory.Insert(club);

           // After the insert the GUID get;s filled with the generator guid.

           Assert.AreNotEqual(club.ClubID, Guid.Empty);

 

           Assert.AreEqual( countriesCount+1 , factory.GetAll().Count);

 

               }