使用 POCO Entity Framework 4 时丢失 DataAnnotations
我将新的 EntityFramework 4.1 与 POCO 对象结合使用,并结合 DataAnnotation 框架。
当EntityFramework需要创建代理类(例如延迟加载虚拟属性)时,我所有的数据注释都丢失了。有什么方法可以让我的 ModelMetaData 来自被代理的类,而不是代理类吗?
我知道我可以选择禁用代理创建(context.Configuration.ProxyCreationEnabled
),但这似乎是一个糟糕的答案。我认为这应该是已经解决的问题。
下面是一些示例代码:
public class Person
{
[Required, Display(Name = "Display That Name")]
public string DisplayName { get; set; }
}
然后在视图中的模型元数据中,类型为: Person_9C92B92D56F6F4F0FB1686526D585438A05323CC72E457C2152796E8866297E1 (FullName = “System.Data.Entity.DynamicProxies.Person_9C92B92D56F6F4F0FB1686526D585438A05323CC72E457C2152796E8866297E1”})
,我的元数据消失了,显示名称在“DisplayName”而不是“显示该名称”处呈现。
有什么建议吗?
I'm using the new EntityFramework 4.1 with POCO objects, in conjunction with the DataAnnotation framework.
When EntityFramework needs to create a proxy class (for example, lazy loading virtual properties), all of my data annotations are lost. Is there some way that my ModelMetaData can come from the class that was proxied, instead of the proxy class?
I know that I have the choice to disable proxy creating (context.Configuration.ProxyCreationEnabled
) but this seems like a poor answer. This should be something that's been solved, I would think.
Here's some example code:
public class Person
{
[Required, Display(Name = "Display That Name")]
public string DisplayName { get; set; }
}
And then in my model metadata in the view, the type is: Person_9C92B92D56F6F4F0FB1686526D585438A05323CC72E457C2152796E8866297E1 (FullName = "System.Data.Entity.DynamicProxies.Person_9C92B92D56F6F4F0FB1686526D585438A05323CC72E457C2152796E8866297E1"})
, my metadata is gone, and the displayname renders out at "DisplayName" not "Display That Name".
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以制作模型类的元数据版本。
我们这样做...EF 生成的内容永远不会被手工触及。
假设您有一个 Person 类:
那么您可以创建 Metadata 类,它保存元数据并且不会被覆盖:
You could make a Metadata version of your model classes.
We do that thisway... what EF generate is never touched by hand.
Let said you have a Person class:
Then you could make the Metadata class, that holds the metadata and won't be overrided:
我们所做的如下:
修改 T4 模板以为您的实体生成部分类。
对于您希望添加注释的实体,创建与您的实体同名的分部类。
在此类中创建一个提供注释详细信息的伙伴类。
应用分部类顶部的属性来指定您的伙伴类是可以找到注释详细信息的位置。
请参阅此处了解更多详细信息 http://msdn.microsoft.com/en-us/库/ee256141.aspx
What we do is as follows:
Modify the T4 templates to generate partial classes for your entities.
For those entities that you wish to add annotations to, create a partial class of the same name of your entity.
Create a buddy class within this class that provides your annotation details.
Apply the attribute at the top of the partial class to specify that your buddy class is where the annotation details can be found.
See here for more details http://msdn.microsoft.com/en-us/library/ee256141.aspx
我想出了一个可能的解决方案。不确定是否有更好的。首先我写了一个新的ModelMetadataProvider:
然后将其注册到Global.asax应用程序启动中:
如果有更好的解决方案,请告诉我!
I figured out one possible solution. Not sure if there are better ones. First I wrote a new ModelMetadataProvider:
And then registered it in Global.asax application start:
If there's a better solution, please let me know!