I\'m trying to make a custom validation [IsUnique]. That check if is property value is unique and return a proper message.
This is my code, but this only work for a spec
When writing Validation Attributes, you can use ValidationContext to gain some information about validation such as Name of Property that you are validating, Type of object that you are validating and so on.
So you don't need to declare which property you want to check for uniqueness, or which entity you should check, or event you don't need to retrieve value using reflection, because the value has been passed to IsValid method.
When using DbContext, you canexecute Sql queries, so you can check for uniqueness using sql query simply. It is more simple than try to Create generic linq query on the fly.
May be this idea help you. Here is some changes in your code according to the idea:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var db = new YourDBContext();
var className = validationContext.ObjectType.Name.Split('.').Last();
var propertyName = validationContext.MemberName;
var parameterName = string.Format("@{0}", propertyName);
var result = db.Database.SqlQuery(
string.Format("SELECT COUNT(*) FROM {0} WHERE {1}={2}", className, propertyName, parameterName),
new System.Data.SqlClient.SqlParameter(parameterName, value));
if (result.ToList()[0] > 0)
{
return new ValidationResult(string.Format("The '{0}' already exist", propertyName),
new List() { propertyName });
}
return null;
}
To use this attribute, simply put [IsUnique] above your property.
[IsUnique]
YourProperty { get; set; }
Then run a test using such code:
var db = new YourDbContext();
db.Configuration.ValidateOnSaveEnabled = true;
db.Categories.Add(new YourEntity() { YourProperty = "DuplicateName" });
db.SaveChanges();
It is a good practice to validate only such aspect of your entity using attributes, that can be validated offline.
Validation Attributes like StringLength, RegularExpression, Required and such validations are examples of good attributes and Validation Attributes that checks for uniqness or other database related rules are examples of inappropriate attributes.