使用 EntityFramework(数据库优先)方法的 DataAnnotations

发布于 2024-11-30 19:35:14 字数 4549 浏览 1 评论 0原文

我有一个项目,其中提供了一个数据库模型类以及一个单独的 EDMX EF 模型。在同一个解决方案中,我有一个 Web 服务,可以访问该项目以及模型类。我希望模型类针对前端执行数据注释以进行验证,但根本没有得到验证。

为了简洁起见,模型类(在我的模型项目中)如下。我的 Web 服务引用此类并用作接口。

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

[DataContract]
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

public class CustomerMetaData
{
    [DataMember]
    public object CustomerID { get; set; }

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

当我点击表单上的“提交”按钮时,它会尝试添加记录并且不进行任何验证。发生运行时错误,通知我需要电子邮件地址。我显然希望通过数据注释预先完成此验证。

我怎样才能做到这一点?

返回一个实际的运行时错误,指出尝试添加记录时电子邮件地址不应为 NULL。这是正确的。数据库列需要一个值。

我认为通过在模型中添加数据注释,如果前端出现问题并且一旦尝试发布表单而模型无效,则应在表单上显示相应的数据注释错误。我的印象是不需要编写任何特定的客户端验证。该模型应该会为您解决这个问题。我的这个假设不正确吗?

网络上有关于如何使用 CodeFirst 执行此操作的文章,但我没有看到关于如何使用 DataBaseFirst 执行此操作的文章。如何才能做到这一点?

我的客户类别再次如下。

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(CustomerMetaData))]
    public partial class Customer
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }

        [DataMember]
        public short CustomerID { get; set; }

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

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Company { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string FirstName { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string LastName { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Address1 { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Address2 { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string City { get; set; }

        [StringLength(2)]
        [DataType(DataType.Text)]
        [DataMember]
        public string State { get; set; }

        [StringLength(10)]
        [DataType(DataType.Text)]
        [RegularExpression(@"^\d{5}(-\d{4})?$")]
        [DataMember]
        public string Zip { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [DataMember]
        public string HomePhone { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [DataMember]
        public string CellPhone { get; set; }

        [StringLength(100)]
        [DataType(DataType.Url)]
        [DataMember]
        public string Website { get; set; }

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

        [DataMember]
        public System.DateTime CreatedDate { get; set; }

        [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }

        public virtual ICollection<Project> Projects { get; set; }
    }
}

当我在客户端中调试“if (ModelState.IsValid)”时,该属性始终返回 true。就好像 DataAnnotations 甚至没有被识别。调试时,我检查 ModelState 对象,它具有所有属性值(在所有情况下都是空字符串,因为我试图强制发生错误)。我故意将其留空的电子邮件地址应该会出现 isRequired 错误。

[HttpPost]
        public ActionResult Create(YeagerTechWcfService.Customer cust)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.AddCustomer(cust);
                    TempData["ErrCode"] = "Customer successfully added.";
                    return RedirectToAction("Index", "Home");
                }
                catch (Exception ex)
                {
                    ViewData["ErrCode"] = "CustErr";
                    ViewBag.Error = ex.Message;
                    return View();
                }
            }
            else
                return View();
        }

I have a project in which I have a database model class provided along with a separate EDMX EF model. In the same solution, I have a web service which accesses this project along with the model class. I want the model class to perform data annotations against the front end for validation, but is not getting validated at all.

For brevity, the model class (in my Model project) is as follows. My web service references this class and is used as the interface.

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

[DataContract]
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

public class CustomerMetaData
{
    [DataMember]
    public object CustomerID { get; set; }

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

When I hit the Submit button on the form, it tries to add the record and does not do any validation. A runtime error occurs informing me that the email address is required. I obviously want this validation to be done up front with the data annotations.

How can I accomplish this?

An actual runtime error is coming back saying that the email address should not be NULL when the record tries to be added. This is correct. The database column requires a value.

I thought that by having the data annotations in the model, if there is something wrong with the front end and the model is not valid once the form tries to be posted, the corresponding data annotation error should display on the form. I was under the impression that there is no need for writing any specific client side validation. The model is supposed to take care of that for you. Am I incorrect in this assumption?

There are articles on the web how to do this with CodeFirst, but I have seen none on how to do this with DataBaseFirst. How can this be accomplished?

Once again, my Customer class is as follows.

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(CustomerMetaData))]
    public partial class Customer
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }

        [DataMember]
        public short CustomerID { get; set; }

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

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Company { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string FirstName { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string LastName { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Address1 { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string Address2 { get; set; }

        [StringLength(50)]
        [DataType(DataType.Text)]
        [DataMember]
        public string City { get; set; }

        [StringLength(2)]
        [DataType(DataType.Text)]
        [DataMember]
        public string State { get; set; }

        [StringLength(10)]
        [DataType(DataType.Text)]
        [RegularExpression(@"^\d{5}(-\d{4})?$")]
        [DataMember]
        public string Zip { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [DataMember]
        public string HomePhone { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [DataMember]
        public string CellPhone { get; set; }

        [StringLength(100)]
        [DataType(DataType.Url)]
        [DataMember]
        public string Website { get; set; }

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

        [DataMember]
        public System.DateTime CreatedDate { get; set; }

        [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }

        public virtual ICollection<Project> Projects { get; set; }
    }
}

When I debug the "if (ModelState.IsValid)" in my client, the property always returns true. It's as if the DataAnnotations are not even being recognized. When debugging, I check the ModelState object and it has all the property values there (an empty string in all cases since I'm trying to force an error). I should be getting an isRequired error on the email address which I'm purposely leaving blank.

[HttpPost]
        public ActionResult Create(YeagerTechWcfService.Customer cust)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.AddCustomer(cust);
                    TempData["ErrCode"] = "Customer successfully added.";
                    return RedirectToAction("Index", "Home");
                }
                catch (Exception ex)
                {
                    ViewData["ErrCode"] = "CustErr";
                    ViewBag.Error = ex.Message;
                    return View();
                }
            }
            else
                return View();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-12-07 19:35:14

不幸的是,这个注释似乎只影响渲染,而不影响验证。我刚刚对 DataType.Url 遇到了同样的问题,并且在问题 DataTypeAttribute 验证在 MVC2 中工作吗?(尽管适用于 MVC 2 - 但问题在 3 中似乎相同)。

只需在其上添加正则表达式数据注释即可:

[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Email was invalid.")]

Unfortunately this annotation only appears to affect rendering, not validation. I've just had the same problem with DataType.Url and it's also discussed in the question Is the DataTypeAttribute validation working in MVC2? (albeit for MVC 2 - but the problems seems the same in 3).

Just drop a regular expression data annotation on it:

[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Email was invalid.")]
不及他 2024-12-07 19:35:14

只是为了对此进行扩展,在 MVC 3 中,您可以采用另一条路线来使用验证。我首先使用 MVC 3 + EF 数据库,我可以在扩展/部分 Model 类中使用这样的代码:

[MetadataType(typeof(Foobar.Metadata))]
[Serializable]
public partial class Foobar
{
    private sealed class Metadata
    {
        [Required]
        [MinLength(10)]
        public object Name { get; set; }
    }

    // Other stuff here
}

然后,当我可以在控制器操作中为我的小 Foobar 提供无效名称时,我可以通过使用 TryValidateModel 来很好地获取验证错误ModelState.IsValid(恐怖中的恐怖,我不进行数据绑定)。

        Foobar c = new Foobar();
        c.Name = "ponies";

        var y = TryValidateModel(c);
        if (!y)
        {
            foreach (var item in ModelState.Values)
            {
                foreach (var err in item.Errors)
                {
                    DoxLog.Error(err.ErrorMessage, err.Exception);
                }
            }
        }

Just to expand on this a bit, in MVC 3 you could go another route to use the validation. I use MVC 3 + EF database first and I can use code like this in the extension/partial Model class:

[MetadataType(typeof(Foobar.Metadata))]
[Serializable]
public partial class Foobar
{
    private sealed class Metadata
    {
        [Required]
        [MinLength(10)]
        public object Name { get; set; }
    }

    // Other stuff here
}

Then when I can feed my little Foobar an invalid name in a controller action and I can get the validation errors nicely by using TryValidateModel instead of ModelState.IsValid (Horror of horrors, I don't databind).

        Foobar c = new Foobar();
        c.Name = "ponies";

        var y = TryValidateModel(c);
        if (!y)
        {
            foreach (var item in ModelState.Values)
            {
                foreach (var err in item.Errors)
                {
                    DoxLog.Error(err.ErrorMessage, err.Exception);
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文