实体框架+的验证注解 [英] Entity framework + validation annotation

查看:109
本文介绍了实体框架+的验证注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个验证anotation。
我已经通过实体框架实现的典范。
我需要数据验证,我知道有一个数据anotation这是非常好的...
但我真的不知道我该如何使用它与实体框架是正确的。

I want to use a validation anotation. I have already a model implemented by entity framework. I need data validation and I know that there is a data anotation which is very nice... But I really don't know how can I use it with entity framework correct.

我应该编辑实体?或者我应该写分开上课吗?或者我应该写这是从实体继承类?

Should I edit entities? Or should I write separated class? Or should I write class which are inherit from entities?

你能告诉我,我应该用最好的方式是什么?

Can you tell me the best way I should use?

我想,因为它是可以写少code。

I want to write as few code as it is possible.

推荐答案

来处理这个问题的方法是局部类的组合,一个特殊的属性,可以让你的元数据附加到其他类。

The way to handle this is a combination of partial classes and a special attribute that allows you to attach metadata to another class.

实体框架已经生成所有类作为局部类在这里帮助你的。所以,如果你有你的模型称为设置一个实体,在EF会造成这样的:

The entity framework already helps you out here by generating all classes as partial classes. So, if you had an entity in your model called Settings, the EF would create this:

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

这意味着你可以有任意数量的其他部分类设置的在其他文件中,这如果/当你重新生成code中的EF不会触及片段。如果你想添加数据验证属性这一点,它需要两个步骤:

This means you can have any number of other partial class Setting fragments in other files, which the EF will not touch if/when you regenerate that code. If you want to add data validation attributes to this, it takes two steps:


  1. 附加属性MetadataType的设置类。这里并不需要在这个部分类片段的身体任何东西,它只是在那里的属性相关联。

  1. Attach a MetadataType attribute to the Setting class. There doesn't need to be anything in the body of this partial class fragment, it's only there to associate the attribute.

[MetadataType(typeof(SettingMetadata))]   
public partial class Setting
{
}


  • 创建具有相同的公共字段名称为EF类,你想要的任何数据验证属性第二类和准。编译器将匹配与名称EF类字段中的元数据类领域,作为它的任何元数据附加到你的第二个类也是在你的头等舱。

  • Create a second class that has the same public field names as the EF class, and associate whatever data validation attributes you want. The compiler will match up the metadata class fields with the EF class fields by name, and act as it whatever metadata is attached to your second class is also on your first class.

    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的SUP pression消息中提到,这个名字的和字段的类型必须在元数据类和相关的类此工作之间不匹配。

    As mentioned in the FxCop suppression message, the name and type of the fields must match between the metadata class and associated class for this to work.

    这篇关于实体框架+的验证注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆