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

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

问题描述

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



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

  public class LogOnModel 
{
[必需]
[Display(Name =User name)]
public string UserName {get;组;

[必需]
[DataType(DataType.Password)]
[显示(名称=密码)]
public string Password {组; }

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

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

  @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 =submitvalue =Create/>
< / p>
< / fieldset>}

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

解决方案

我没有为ASP.NET MVC(仅适用于Silverlight)同样的原则也适用。您可以创建如下所示的元数据好友类,因为EF生成的类型应该是部分的,因此您可以添加更多的元素(如MetadataTypeAttribute),然后创建保存元数据的同级类。 p>

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

  [MetadataType(typeof(PersonMetadata))] 
public partial class Person {
//注意这个类没有任何内容。只是在这里添加类级属性。
}

public class PersonMetadata {
//将名称与EF相同的字段命名为属性 - 例如FirstName。
//另外,类型需要匹配。基本上只是重新发音。
//注意这是一个字段。我认为它也可以是一个财产,但领域肯定应该有效。

[必需]
[显示(名称=名字)]
public string 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;
}

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

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