MVC5网站新手/自动生成模型属性的注释(MVC5 website newbie / Annotations on properties of autogenerated modell)

编程入门 行业动态 更新时间:2024-10-05 11:23:30
MVC5网站新手/自动生成模型属性的注释(MVC5 website newbie / Annotations on properties of autogenerated modell)

我是mvc 5的新手,我想构建一个asp.net应用程序来与现有数据库进行交互。 我正在使用VIsual studio 2013和Entity Framework 6。

我从现有数据库生成了一个ADO.net实体数据模型,我目前正在尝试找出进行数据验证的最佳方法,以避免错误的输入(让我们以实体用户的字段电子邮件为例)。 正确的方法似乎是在部分类上使用Annotations。 但我不知道如果原始属性声明在自动生成的文件上,如何添加注释(在我为其创建的新的分部类上)。

自动生成的类,如下所示:

namespace Test.Models { ... public partial class Users { public string Email { get; set; } } ... }

按照[这篇文章]背后的想法( 将数据注释添加到实体框架生成的类 ),我正在尝试创建一个部分类来编写注释,就像那样:

namespace Test.Models { using System.ComponentModel.DataAnnotations; [MetadataType(typeof(UsersMetaData))] public partial class Users { [Someanotations] public string Email { get; set; } } }

但是在部分类中,我得到:1)“[MetadataType(typeof(UsersMetadata))]”行的错误,说无法找到UsersMetadata,2)“public string email”行上的错误,说该属性已经宣布(这听起来对我而言)。

我应该如何在新的分类上注释在自动生成的模型上声明的属性? 可以定义要在anotation上使用的正则表达式吗?

谢谢你的时间,

约翰

i'm new to mvc 5, and I would like to build an asp.net application to interact with an existing database. I'm using VIsual studio 2013 and Entity Framework 6.

I've generated an ADO.net Entity Data Model from an existing database and I'm currently trying to find out the best way to make data validations, to avoid wrong inputs (let's take as example the field Email from entity Users). The right way seems to be to use Annotations on partial classes. But i don't know how to add an annotation (on the new partial class that i created for that) if the original property declaration is on the autogenerated file.

The autogenerated class, looks like:

namespace Test.Models { ... public partial class Users { public string Email { get; set; } } ... }

Following the idea behind [this post] (Add data annotations to a class generated by entity framework), i'm trying to make a partial class to write the annotations there, like that:

namespace Test.Models { using System.ComponentModel.DataAnnotations; [MetadataType(typeof(UsersMetaData))] public partial class Users { [Someanotations] public string Email { get; set; } } }

But on the partial class, i get: 1) Error on the line of "[MetadataType(typeof(UsersMetadata))]", saying that UsersMetadata could not be found, and 2) Error on the line where "public string email", saying that the property is already declared (which sounds logic for me).

How should i annotate on the new partial class the property that is declared on the autogenerated model? It is possible to define a Regex to be used on the anotation?

Thanks for your time,

John

最满意答案

你快到了。 UserMetadata实际上是您应用注释的另一个类。 我通常将这两个放在同一个文件中。

[MetadataType(typeof(UsersMetaData))] public partial class Users { } class UsersMetaData { [Someanotations] public string Email { get; set; } }

You're almost there. UserMetadata is actually another class that you apply the annotations to. I usually put both of these in the same file.

[MetadataType(typeof(UsersMetaData))] public partial class Users { } class UsersMetaData { [Someanotations] public string Email { get; set; } }

更多推荐

本文发布于:2023-08-07 17:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   自动生成   属性   模型   新手

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!