使用System.ComponentModel.DataAnnotations实体框架4.0 [英] Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

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

问题描述

我与MVC3工作,并使用实体框架4.0 Entities作为我的模型。到目前为止,一切都很正常,只要使用它作为一个模型(所有CRUD操作/页几代人的工作开箱即用)。我不知道,但是,你如何获得同样强大的标签和验证信息,当您手动生成一个模型?

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?

下面是我的意思的例子。这是由样本MVC3项目生成一个类:

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>}

我的问题是:如何添加这些额外的装饰到使用EF4生成的实体?有没有除了我应该使用System.ComponentModel.DataAnnotations的东西吗?我知道实体得到再生,它ç直接可能不是把它添加到实体的$ C $个好主意,但由于某种原因,我想不出更好的方法比在视图中手动输入标签文本(瘸子,还有没有理由要做到这一点,这是MVC的!)。我想保持它,这样应用程序是足够的动态​​,以便能够对我的模型正确的显示信息来通过,并保持一个MVC的方法。我该怎么做呢?

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?

推荐答案

我没有这样做的ASP.NET MVC(仅适用于Silverlight的),但我相信同样的原则也适用。您可以创建如下一个元数据哥们班,因为EF产生的类型应该是局部的,所以你可以多一点添加到它们(如MetadataTypeAttribute),然后创建一个保存元数据,这个​​兄弟类。

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.

这是一种难看,但应该工作。它是这样的(假设EF实体被命名为人):

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;
}

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

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