EF和DataBaseFirst的DataAnnotatons不起作用

时间:2011-09-09 19:06:40

标签: entity-framework-4.1

我已经修改了我的模型中的代码(在一个单独的层上),元数据希望这可以解决问题,但事实并非如此。就好像模型中的DataAnnotations没有被拾取一样?为什么???

查看页面源时,我应该看到如下内容:

data-val-required="The User name field is required."

然而,我所看到的只是fiollowing:

<div class="editor-label">




            <label for="Email">Email</label>




        </div>




        <div class="editor-field">




            <input id="Email" name="Email" type="text" value="" />




            <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>




        </div>

查看标准帐户登录模型时,我有以下内容:

YeagerTech.Models.LogOnModel

以上内容在AccountModels.cs文件内网站根目录下的Models文件夹中定义:

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

以上(虽然模型在网站前端,而不在另一层)工作正常,并在页面源中设置了所需的属性。我需要做什么才能执行数据验证我的模型驻留在另一层的EF DataBaseFirst方法? 我的web.config文件设置如下:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

在前端,我的视图设置如下:

@model YeagerTech.YeagerTechWcfService.Customer

@{
    ViewBag.Title = "Create Customer";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("Create", "Customer", new AjaxOptions { HttpMethod = "POST"}))
{
    @Html.ValidationSummary(true, "Create was unsuccessful. Please correct the errors and try again.") 
    <fieldset>
        <legend>Customer</legend>

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

YeagerTech.YeagerTechWcfService.Customer的定义设置如下(在Reference.cs文件中)

[System.Diagnostics.DebuggerStepThroughAttribute()]     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]     [System.Runtime.Serialization.DataContractAttribute(Name="Customer", Namespace="http://schemas.datacontract.org/2004/07/YeagerTechModel")]     [System.SerializableAttribute()]     public partial class Customer : YeagerTech.YeagerTechWcfService.CustomerDA {     }

我的模型在上述服务中设置如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace YeagerTechModel
{
    [Serializable]
    [DataContract]
    [MetadataType(typeof(CustomerDA))]
    public partial class Customer : CustomerDA
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }
    }

    [Serializable]
    [DataContract]
    public class CustomerDA 
    {  
        [DataMember]
        public short CustomerID { get; set; }

        [Required]
        [StringLength(50)]
        [DataType(DataType.EmailAddress)]
        [DataMember]
        public string Email { get; set; }

基础DbContext类

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace YeagerTechModel
{
    public partial class YeagerTechEntities : DbContext
    {
        public YeagerTechEntities()
            : base("name=YeagerTechEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Customer> Customers { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

我从您的代码中看到您添加了jquery.validate.js脚本和jquery.validate.unobtrusive.js script。但是,我没有看到包含jquery脚本。您必须包含jquery,它必须是第一个脚本。此外,由于您使用的是MVC的Ajax功能,因此您还需要包含jquery.unobtrusive-ajax.js脚本。