在 Entity Framework 4.0 中使用 System.ComponentModel.DataAnnotations [英] Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

查看:21
本文介绍了在 Entity Framework 4.0 中使用 System.ComponentModel.DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVC3,并使用 Entity Framework 4.0 实体作为我的模型.到目前为止,只要将其用作模型,一切都很好(所有 crud 操作/页面生成都开箱即用).不过,我想知道,您如何获得与手动生成模型时相同的强大标签和验证信息?

这是我的意思的一个例子.这是示例 MVC3 项目生成的类:

公共类LogOnModel{[必需的][Display(Name = "用户名")]公共字符串用户名 { 获取;放;}[必需的][数据类型(数据类型.密码)][显示(名称=密码")]公共字符串密码{获取;放;}[Display(Name = "还记得我吗?")]公共布尔记住我{得到;放;}}

在上面的示例中,您可以指定在标签中为字段 (Display) 呈现的内容,以及要使用的字段类型(密码).但是,当我尝试使用实体框架并将其推送到下面的视图时,我看到自动生成的标签只是字段名称,而不是我希望用户看到/必须阅读的任何内容:

@using (Html.BeginForm()) {@Html.ValidationSummary(true)<字段集><legend>人物</legend><div class="editor-label">@Html.LabelFor(model =>model.FirstName)

<div class="editor-field">@Html.EditorFor(model => model.FirstName)@Html.ValidationMessageFor(model =>model.FirstName)

<div class="editor-label">@Html.LabelFor(model => model.MiddleName)

<div class="editor-field">@Html.EditorFor(model => model.MiddleName)@Html.ValidationMessageFor(model => model.MiddleName)

<div class="editor-label">@Html.LabelFor(model => model.LastName)

<div class="editor-field">@Html.EditorFor(model => model.LastName)@Html.ValidationMessageFor(model =>model.LastName)

<div class="editor-label">@Html.LabelFor(model => model.Birthdate)

<div class="editor-field">@Html.EditorFor(model => model.Birthdate)@Html.ValidationMessageFor(model => model.Birthdate)

<p><输入类型=提交"值=创建"/></p></fieldset>}

我的问题是:如何将这些额外的装饰添加到使用 EF4 生成的实体中?除了 System.ComponentModel.DataAnnotations 之外,还有什么我应该使用的吗?我知道实体会重新生成,直接将其添加到实体的代码中可能不是一个好主意,但出于某种原因,我想不出比在视图中手动输入标签文本更好的方法(蹩脚,没有理由必须这样做,这是MVC!).我想保留它,以便应用程序足够动态,以便能够为我的模型提供正确的显示信息,并保持 MVC 方法.我该怎么做?

解决方案

我还没有为 ASP.NET MVC(仅适用于 Silverlight)这样做,但我相信同样的原则也适用.您可以创建一个元数据伙伴类",如下所示,因为 EF 生成的类型应该是部分类型,因此您可以向它们添加更多(如 MetadataTypeAttribute),然后您创建这个保存元数据的兄弟类.

这有点难看,但应该有效.它是这样的(假设 EF 实体名为Person"):

[MetadataType(typeof(PersonMetadata))]公共部分类人{//注意这个类里面什么都没有.这里只是添加类级别的属性.}公共类 PersonMetadata {//将字段命名为与 EF 命名的属性相同的名称 - 例如FirstName".//另外,类型需要匹配.基本上只是重新声明它.//注意这是一个字段.我认为它也可以是一个属性,但字段绝对应该起作用.[必需的][显示(名称=名字")]公共字符串名字;}

I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So far, everything works great as far as using it as a model (all the crud operations/page generations work out of the box). I'm wondering, though, how do you get the same robust labels and validation information as when you generate a model manually?

Here's an example of what I mean. This is a class generated by the sample MVC3 project:

public class LogOnModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

With the example above, you can specify what gets rendered in a label for the field (Display), and what type of field to use (Password). However, when I try to use the entity framework and push it to the view below, I see the automatically generated labels are just the field names, and not anything I want the user to see/have to read:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Person</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.MiddleName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MiddleName)
            @Html.ValidationMessageFor(model => model.MiddleName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Birthdate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Birthdate)
            @Html.ValidationMessageFor(model => model.Birthdate)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>}

My question is: How do I add these extra decorations to the entities that are generated using EF4? Is there something besides System.ComponentModel.DataAnnotations that I should be using? I know entities get regenerated and it's probably not a good idea to add this to entities' code directly, but for some reason I can't think of a better approach than manually entering the label text in the view (lame, there's no reason to have to do that, this is MVC!). I want to keep it so that the application is dynamic enough to be able to have the correct display information for my model come through and keep an MVC approach. How do I do it?

解决方案

I haven't done this for ASP.NET MVC (only for Silverlight) but I believe the same principles would apply. You can create a "metadata buddy class" as below, because the types generated by EF should be partial, thus you can add a bit more to them (like the MetadataTypeAttribute) and then you create this sibling class that holds the metadata.

It's kind of ugly, but should work. It goes something like this (assuming the EF entity is named "Person"):

[MetadataType(typeof(PersonMetadata))]
public partial class Person { 
  // Note this class has nothing in it.  It's just here to add the class-level attribute.
}

public class PersonMetadata {
  // Name the field the same as EF named the property - "FirstName" for example.
  // Also, the type needs to match.  Basically just redeclare it.
  // Note that this is a field.  I think it can be a property too, but fields definitely should work.

   [Required]
   [Display(Name = "First Name")]
  public string FirstName;
}

这篇关于在 Entity Framework 4.0 中使用 System.ComponentModel.DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆