c# - Using CustomValidationAttribute with Entity Framework -


i want implement conditional validation logic on 1 of entities. basically, want validate field (using regular expression) if field in entity has value.

i tried adding customvalidationattribute on entity doesn't anything

[metadatatype(typeof(templateactionmetadata))] [customvalidation(typeof(templateaction), "validateactionvalue")]     public partial class templateaction      {         public static validationresult validateactionvalue(string pvalue, validationcontext vcontext)         { //this never gets called             return validationresult.success;         }     } 

i tried adding attribute on field

public partial class templateactionmetadata     {          [customvalidation(typeof(templateaction), "validateactionvalue")]         public string actionvalue { get; set; }     } 

now call validateactionvalue method validationcontext parameter null, have no way of getting value of other field need use implement validation logic.

in msdn documentation, can see customvalidationattribute class has readonly requiresvalidationcontext property.

this property overridden base class validationattribute.

since don't have context, suppose property return false, design.

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute.aspx

so can't context attribute. may have create custom attribute fulfill needs

i found microsoft guideline you: https://msdn.microsoft.com/en-us/library/cc488527.aspx


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -