添加验证与数据库优先模型到模型(EF 5) [英] Adding validation to model with Database First model (EF 5)

查看:109
本文介绍了添加验证与数据库优先模型到模型(EF 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何验证错误添加到模型状态。我知道如何验证注释添加到我的模型类。问题是,与数据库首先,​​我不想碰产生code,因为当我再生,我会失去我的定制。我总是试图在局部模板定制,但你不能在部分添加注释到现有的属性。

I know how to add validation errors to the model state. I know how to add the validation annotations to my model classes. The problem is that with Database first, I don't want to touch the generated code, because when I regenerate, I will lose my customization. I always try to customize in partials, but you can't add annotation to an existing property in a partial.

什么是最好的做法在这里?

What is best practice here?

推荐答案

您需要采取 MetadataTypeAttribute

做这样的事情:

创建一个新的类文件,保存在相同的命名空间的局部类。这个新类将保持你的验证规则,即使你从更新数据库模型。修改新类文件的内容如下图所示,改变你的规格等。

Create a new class file, keep it in the same namespace as your partial class. This new class will keep your validation rules even if you update your Model from Database. Modify the contents of your new class file like below, change to your specifications ,etc.

[MetadataTypeAttribute(typeof(YourCustomClassForValidation))]
public partial class Person
{
   // No need to put anything here because you already defined these properties
}

public class YourCustomClassForValidation
{
   [DisplayName("Full Name")]
    public string name { get; set; }
}

这篇关于添加验证与数据库优先模型到模型(EF 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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