使用 DataAnnotations 验证复杂类型
我决定在我的项目中使用实体框架进行 O/R 映射,并使用 DataAnnotations 进行验证,现在在尝试实现此功能时遇到了一个奇怪的问题。
这就是我所做的:
我有以下实体类型
Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null)
Address Address (not null)
String Phone
String Email
,其中 Name
和 Address
是定义如下的复杂类型:
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
项目时,Name
和 Address
类型将填充 Name
的实例> 和 Address
,其中所有值均为 null
,而不是 Name
和 Address
具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么它创建了属性为 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.确保 HTML 字段中最终出现的名称与类的属性名称对齐。
例如,如果您有以下内容:
您对 HTML 帮助程序的调用应如下所示:
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:
Your calls to the HTML helpers should look like this:
查看这篇博文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.
我现在正在努力解决同样的问题。 我认为一种半优雅的方法是将关键原始类型引用为属性,并在其上添加数据注释。 下面是一个以 AddressID 为关键字段的示例。
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.