This is a migrated thread and some comments may be shown as answers.

Unique constraint with data annotation

1 Answer 278 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
RABIE
Top achievements
Rank 1
RABIE asked on 06 Nov 2013, 01:54 PM
I'm using the System.ComponentModel.DataAnnotations namespace to validate my domain classes. How can I create a custom attribute to validate the uniqueness of Name and Email property regardless of the database 


public partial class T_User
{
    private int _idUser;
    [Required()]
    [Key()]
    public virtual int IdUser
    {
        get
        {
            return this._idUser;
        }
        set
        {
            this._idUser = value;
        }
    }
     
    private string _name;
    [StringLength(50)]
    [Required]
    public virtual string Name
    {
        get
        {
            return this._name;
        }
        set
        {
            this._name = value;
        }
    }
     
    private string _mail;
    [StringLength(50)]
    [Required]
    [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Adresse mail non valide !")]
   
    public virtual string Mail
    {
        get
        {
            return this._mail;
        }
        set
        {
            this._mail = value;
        }
    }
     
    private string _password;
     
    [StringLength(50)]
    [Required]
    public virtual string Password
    {
        get
        {
            return this._password;
        }
        set
        {
            this._password = value;
        }
    }
     
    private int? _idRole;
    public virtual int? IdRole
    {
        get
        {
            return this._idRole;
        }
        set
        {
            this._idRole = value;
        }
    }
     
    private T_Role _t_Role;
    public virtual T_Role T_Role
    {
        get
        {
            return this._t_Role;
        }
        set
        {
            this._t_Role = value;
        }
    }
     
    private IList<R_SiteUser> _r_SiteUsers = new List<R_SiteUser>();
    public virtual IList<R_SiteUser> R_SiteUsers
    {
        get
        {
            return this._r_SiteUsers;
        }
    }
     
    private IList<R_PrestationUser> _r_PrestationUsers = new List<R_PrestationUser>();
    public virtual IList<R_PrestationUser> R_PrestationUsers
    {
        get
        {
            return this._r_PrestationUsers;
        }
    }
     
}

1 Answer, 1 is accepted

Sort by
0
Kristian Nikolov
Telerik team
answered on 08 Nov 2013, 02:58 PM
Hello Rabie,

You can define a custom DataAnnotation attribute which would validate the uniqueness of the the property to which it is applied.

I have attached a sample implementation of such attribute class. It checks if there are entities of the same type with the same property value in the database. If there are such entities their primary keys are evaluated against that of the entity which property is currently being validated. Please note that this implementation would access the database each time a property is being validated and this may lower the performance of your application.

Since such attribute would normally be applied to a small number of different properties, we recommend adding it manually to the properties of your persistent classes. In case you are using a Domain Model which would regenerate its persistent classes and context each time it is saved, it may be a good idea to write a unit test which checks if the required properties have the attribute added. The sample Unique attribute can be applied to a property as follows:
[Unique("TestDataAnnotations.EntitiesModel")]
public virtual string TagNumber { get; set; }

As an argument, the full name of the model`s context class is passed. In the code snippet above, "TestDataAnnotations" is the namespace containing the model and "EntitiesModel" is the name of the model`s context class.

Note that even when validating the uniqueness of a value in your application, it is still highly recommended to specify the respective column as unique. This will prevent other applications that may work with the same database from inserting invalid values.

I hope this helps.

Regards,
Kristian Nikolov
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Tags
General Discussions
Asked by
RABIE
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Share this question
or