csh*_*oob 2 asp.net-mvc entity-framework custom-validators
在ScottGu的博客上是一个如何在EF4中使用MVC2自定义验证的示例:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
所以这里的问题:
当VS2010中的Designer为数据库创建对象时,您必须向该类添加[MetadataType(typeof(Person_validation))] Annotation.
但是当我在Designer中更改任何内容时,所有这些注释都会丢失.
是否可以对edmx文件进行自我更改,或者是否有更好的方法将System.ComponentModel.DataAnnotations应用于生成的实体?
谢谢.
你用一种松散称为"伙伴类"的模式来做到这一点.基本上你要做的是用元数据创建一个单独的类,并创建一个将生成的实体耦合到你的伙伴类的部分类.
举一个简单的例子,假设你有一个Person
实体,并且你想要将FirstName
属性设置为必需的.这是您在生成的文件之外执行的操作:
[MedadataType(typeof(PersonMetadata))]
partial class Person { } // the other part is generated by EF4
public class PersonMetadata
{
// All attributes here will be merged into the generated class,
// thanks to the partial class above. Just apply attributes as usual.
[Required]
public string FirstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
你可以找到这种方法的更多的细节在这里.在你链接的文章中,ScottGu实际上也在谈论它.请看标题"第5步:坚持使用数据库";)
归档时间: |
|
查看次数: |
1266 次 |
最近记录: |