1

I'm using Entity Framework code-first and so I make liberal use of Data Annotations. I use the [Key] annotation, which is from sealed class KeyAttribute and noticed that it has no client-side validation as it inherits from the Attribute class and not the ValidationAttribute class.

Because the class is sealed, I can't inherit from it. Is there another way to add validation through data annotation without simply making another attribute that mimics [Key]?

(I'm attempting to avoid something like [Key, KeyValidation("There may only be one!")])

Charles W
  • 2,262
  • 3
  • 25
  • 38
  • What exactly do you need to validate? A primary key is unique. If it is an identity then you have nothing to validate. – Dismissile Mar 17 '14 at 13:06
  • 2
    I don't think that you should be letting your users/front-end interact with your primary keys in any way. This could cause a lot of issues. Can you explain why you are trying to do this? – Chris Mar 17 '14 at 13:16
  • @Chris Essentially I'd like to give a user full-control. I understand the risks, however that's kind of the idea behind the validation. A user wouldn't be able to commit changes to the actual database if their primary keys are highlighted red. – Charles W Mar 17 '14 at 13:25
  • it seems Microsoft didn't think about if someone wants to give users the control of the unique/primary-key field. it seems you need your own custom one for this case. – Zafar Mar 17 '14 at 14:10
  • How to check for duplicates: http://stackoverflow.com/a/18736484/150342 – Colin Mar 18 '14 at 10:30

0 Answers0