是否可以动态地向实体对象添加属性?
是否可以在运行时向实体框架生成的类添加属性?我成功地使用从 DynamicObject 继承的 POCO 类执行此操作,但是当我尝试对实体类执行相同的操作时,我收到错误“[类名] 的部分声明不得指定不同的基类”。
is it possible to add properties at run-time to a class that was generated by the entity framework? I am successful in doing so with POCO classes that inherit from DynamicObject but when I try to do the same thing with my entity classes, I receive the error "Partial declarations of [class name] must not specify different base classes".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,我很惊讶它能与 L2S 配合使用;毕竟,
DynamicObject
是一头好奇的野兽。您可以尝试确保使用 EF 4 POCO 支持,而不是 3.5SP1 风格的公共基类方法,但说实话,我不确定这是正确的方法...数据库对象不应该真正拥有 动态特性,IMO。您的意图尚不清楚,但如果这是出于数据绑定目的,您可能需要通过
ICustomTypeDescriptor
或TypeDescriptionProvider
查看自定义对象模型。这是一种较旧的技术,似乎可以在运行时扩展对象(不需要动态
)。I'm actually pretty surprised that works with L2S;
DynamicObject
is a curious beast, after all. You could try ensuring you are using the EF 4 POCO support, rather than the 3.5SP1-style common-base-class approach, but to be honest I'm not sure this is the way to go... database objects shouldn't really have dynamic properties, IMO.Your intent isn't clear, but if this is for data-binding purposes, you might want to look at custom object models, via
ICustomTypeDescriptor
orTypeDescriptionProvider
. This is an older technology for seeming to extend objects at runtime (without needingdynamic
).