Entity framework + annotation for verification

I want to use validation annotation. I already have a model implemented by entity infrastructure. I need data validation, and I know that there is a data announcement that is very nice ... But I really don’t know how I can use it with the correct entity structure.

Do I have to edit objects? Or should I write a separate class? Or should I write a class that inherits from entities?

Can you tell me what is the best use?

I want to write as little code as possible.

+3
source share
2 answers

- , .

, . , "", EF :

public partial class Setting : INotifyPropertyChanging, INotifyPropertyChanged
{
    // Auto-gen Properties, methods, etc go here.
}

, partial class Setting , EF , / . , :

  • MetadataType Setting. , , .

    [MetadataType(typeof(SettingMetadata))]   
    public partial class Setting
    {
    }
    
  • , , EF, , . EF , , , .

    public class SettingMetadata
    {
        [Display(Name="Base Rate")]
        [Required]
        public decimal Rate
        {
            get;
            set;
        }
    
        [Display(Name = "Permit Payments")]
        public Boolean AllowPayments
        {
            get;
            set;
        }
    
        [Display(Name = "Base URL For Web Service")]
        [Required]
        [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Type must match linked metadata type.")]
        public string WebServiceUrl
        {
            get;
            set;
        }
    }
    

FxCop, , .

+14

- , . , , .

.

+2

All Articles