as @LBushkin mentioned, Attributescompile-time constants are needed.
I would change your class:
public class UniqueAttribute : ValidationAttribute
at
public class UniqueAttribute<T> : ValidationAttribute
where T : DataContext{
protected T Context { get; private set; }
...
}
and use it like:
[Required]
[StringLength(10)]
[Unique<DataContext>("Groups","name")]
public string name { get; set; }
This will help you add a DataContext if necessary instead of instantiating each time.
NTN
Edit: since an attribute cannot accept a common parameter, this could be another potential code:
public class UniqueAttribute : ValidationAttribute{
public UniqueAttribute(Type dataContext, ...){
if(dataContext.IsSubClassOf(typeof(DataContext))){
var objDataContext = Activator.CreateInstance(dataContext);
}
}
}
and use it like:
[Required]
[StringLength(10)]
[Unique(typeof(DataContext), "Groups","name")]
public string name { get; set; }
Hth this time :)
Sunny source
share