How can I create a generic UniqueValidationAttribute in C# and DataAnnotation?

后端 未结 4 742
感动是毒
感动是毒 2021-02-03 15:44

I\'m trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute

I want this to be generic as in I c

4条回答
  •  遇见更好的自我
    2021-02-03 16:07

    One problem I see already is that you can't instantiate types as parameters of attributes.

    Attributes require that all arguments be compile-time constants. So the usage:

    [Unique(new DataContext(),"Groups","name")]
    

    won't compile. You may be able to omitt new DataContext() - but then I suspect your validation logic won't have information about the entity types to query.

提交回复
热议问题