使用实体框架数据库优先方法生成模型时自动进行模型验证 [英] Automatic Model Validation When Generating the Models using Entity Framework Database First Approach

查看:111
本文介绍了使用实体框架数据库优先方法生成模型时自动进行模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下模型类的属性UserName。您可以看到验证条件是手动添加的。

Consider the following property UserName of a Model Class. You can see that the validation criteria are added over it manually.

[Required]
[StringLength(100, MinimumLength = 6)]
public string UserName { get; set; }

现在再次考虑以下代码:

Now again consider the following code:

public string UserName { get; set; }

没有验证者的相同属性。现在,当我使用Entity Framework Database的第一种方法生成模型类时,我得到的是后面的结果(表示没有验证器的属性)。但是在数据库中,每个属性上都添加了约束。

The same property without the validators. Now when I am generating the model class using Entity Framework Database first approach I am getting the later result (means a property without having validators). But in the database there are constraints added over each attribute.

因此,有没有什么工具/方法可以使用这些约束并生成具有如下所示属性的模型类:代码1(具有验证程序的属性)。

So is there any tool/way that I can use those constraints and generate the model class having properties like shown in code 1 (that is property with validators).

推荐答案

没有可用的工具可以为您添加这些属性。您可以修改T4模板来为您创建这些属性,但是为此您需要了解EF元数据的存储方式。您可以在伙伴类中手动手动添加属性

No there is no ready to use tool which would add these attributes for you. You can modify T4 template to create these attributes for you but for that you need to understand how EF metadata are stored. You can add attributes yourselves manually in buddy classes.

这篇关于使用实体框架数据库优先方法生成模型时自动进行模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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