AdFactum ObjectMapper .NET Blog

Official blog of the AdFactum ObjectMapper .NET

Attribute: [Unique]

Posted by Gerhard Stephan on September 20th, 2006

Using the Unique Attribute on a property tells the ObjectMapper .NET to create an unique constraint when exporting the DDL file. The ObjectMapper .NET allows different ways to use the Unique Attribute.

Create a single unique property:

First of all you can create a single unique property. This can be done by attributing the property with the Unique attribute.

            [Unique]

            [PropertyLength(7)]

            public string PersonnelNumber

            {

                  get { return _personnelNumber;}

                  set { _personnelNumber = value;}

            }

The resulting DDL script would look like:

ALTER TABLE EMPLOYEE ADD CONSTRAINT EMPLOYEE_UK01 UNIQUE (PERSONNELNUMBER);

Create unique key groups:

As an other feature, you can group unique keys and bundle them to one unique key index. This is extremly useful because not every property is unique by itself. A unique key group has a number that identifies the group. The number itself is insignificant, it’s only important  that the properties you want to group have the same key group number assigned.

        [PropertyLength(5)]

        [Unique(1)]             // Key Group 1 has been set

        public string Locale

        {

            get { return locale; }

            set { locale = value; }

        }

 

        [PropertyLength(32)]

        [Unique(1)]             // Key Group 1 has been set

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

The resulting DDL script would look like:

ALTER TABLE [TRANSLATION] ADD CONSTRAINT TRANSLATION_UK01 UNIQUE ([LOCALE],[KEY]);

Create unique key groups with sorted keys:

Additional to the unique key group feature you can add an sorting to the key group. Look at our example above. Most times it is necessary to store the properties in a defined sorting. E.g. You always need it if you want to build business logic driven foreign keys that rely on a combined unique key index.

In that case, the second number, after the key group defines the sorting of the keys. Imagine you want to change the ordering of the keys in our example above. Than it would look like:

        [PropertyLength(5)]

        [Unique(1,2)]             // Key Group 1 has been set

        public string Locale

        {

            get { return locale; }

            set { locale = value; }

        }

 

        [PropertyLength(32)]

        [Unique(1,1)]             // Key Group 1 has been set

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

 

The resulting DDL script would look like:

ALTER TABLE [TRANSLATION] ADD CONSTRAINT TRANSLATION_UK01 UNIQUE ([KEY], [LOCALE]);

You see that the sorting of the columns within the unique key index changed.

Summary:

Some special things, like function based unique key constraints, should be better left to a seasoned database admin. Additionally such things are not easy to maintain by source code. But I think that the unique key attribute offers you a lot possibilities to cover more than 90% of all use cases, for which you might need unique key constraints.

Cheers
Gerhard

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>