使用 DataAnnotations 验证复杂类型

发布于 2024-07-25 17:08:24 字数 1170 浏览 9 评论 0原文

我决定在我的项目中使用实体框架进行 O/R 映射,并使用 DataAnnotations 进行验证,现在在尝试实现此功能时遇到了一个奇怪的问题。

这就是我所做的:

我有以下实体类型

Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null) 
Address Address (not null)
String Phone
String Email

,其中 NameAddress 是定义如下的复杂类型:

Name                                  Address
****                                  *******
String First (not null)               String Street (not null)
String Last (not null)                String ZipCode (not null)
                                      String City (not null)

并且以下类驻留在与我的名称空间相同的名称空间中实体:

public class ContactMetadata
{
    [Required]
    public Name Name { get; set; }
}

[MetadataType(typeof(ContactMetadata))]
partial class Contact { }

但是,当我创建新的 Contact 项目时,NameAddress 类型将填充 Name 的实例> 和 Address,其中所有值均为 null,而不是 NameAddress 具有 null > 价值观本身。 因此,尽管所有值都是 null,但 Required 属性不会引发任何错误。 我该如何解决这个问题?

I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in my project, and I have now encountered an odd problem when trying to implement this.

This is what I've done:

I have the following entity type

Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null) 
Address Address (not null)
String Phone
String Email

where Name and Address are complex types defined as follows:

Name                                  Address
****                                  *******
String First (not null)               String Street (not null)
String Last (not null)                String ZipCode (not null)
                                      String City (not null)

And the following classes reside in the same namespace as my entities:

public class ContactMetadata
{
    [Required]
    public Name Name { get; set; }
}

[MetadataType(typeof(ContactMetadata))]
partial class Contact { }

However, when I create a new Contact item, the Name and Address types are filled with instances of Name and Address where all values are null, instead of Name and Address having null values themselves. Thus, the Required attribute doesn't throw any errors, although all values are null. How do I work around this?

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

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

发布评论

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

评论(4

公布 2024-08-01 17:08:24

那么它创建了属性为 null 的 Name 和 Address 对象的实例? 有趣的。

您可以将 [Required] 属性放在孩子身上吗?

编辑:我知道这可能被认为是一种臭方法,但为了清楚起见,我将您的答案编辑到帖子中,以便下一个遇到此问题的人可以更容易地找到它...

建议(已接受,但尚未测试)解决方案:

编写一个自定义验证属性,用于针对 null 值进行验证。

So it creates instances of the Name and Address objects who's properties are null? Interesting.

Can you just put the [Required] attribute on the children?

EDIT: I know this might be considered a smelly way of doing this, but for clarity I edit your answer into the post, so that it can easier be found by the next person having problems with this...

Suggested (and accepted, but yet untested) solution:

Write a custom validation attribute which validates against the null values.

泅人 2024-08-01 17:08:24

确保 HTML 字段中最终出现的名称与类的属性名称对齐。

例如,如果您有以下内容:

public class Contact {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    [...]
}

您对 HTML 帮助程序的调用应如下所示:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Address.Street") %>
<%= Html.TextBox("Address.City") %>
[...]

Make sure the names that end up in the HTML fields line up with the property names of the class.

For example, if you have this:

public class Contact {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    [...]
}

Your calls to the HTML helpers should look like this:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Address.Street") %>
<%= Html.TextBox("Address.City") %>
[...]
青春有你 2024-08-01 17:08:24

查看这篇博文complex-dataannotations-validation。 我认为 requiredAssociation 属性正是您所需要的。 您可能需要针对实体框架而不是 LINQ to SQL 稍微调整一下。

Check out this blog post complex-dataannotations-validation. I think the RequiredAssociation attribute is what you need. You might have to tweak it a little bit for Entity Framework instead of LINQ to SQL.

野生奥特曼 2024-08-01 17:08:24

我现在正在努力解决同样的问题。 我认为一种半优雅的方法是将关键原始类型引用为属性,并在其上添加数据注释。 下面是一个以 AddressID 为关键字段的示例。

public class Contact{

[Required]
public int? AddressIDForValidation{
get{return this.Address.AdressID;}
}

public Address Address{get;set;}
}



public class Address{
public int? AddressID{get;set;}
public string Street{get;set;}
}

I'm struggling with the same issue right now. I think a semi-graceful way to do it is to reference the key primitive type as a property, and put the dataannotation on that. Here's an example with AddressID being the key field.

public class Contact{

[Required]
public int? AddressIDForValidation{
get{return this.Address.AdressID;}
}

public Address Address{get;set;}
}



public class Address{
public int? AddressID{get;set;}
public string Street{get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文