Entity Framework 代理类 的必要条件(msdn原文+翻译)
原文:
The Entity Framework creates proxies for POCO entities if the classes meet the requirements described below. POCO entities can have proxy objects that support change tracking or lazy loading. You can have lazy loading proxies without meeting the requirements for change tracking proxies, but if you meet the change tracking proxy requirements, then the lazy loading proxy will be created as well. You can disable lazy loading by setting the LazyLoadingEnabled option to false.
简单来说代理类是跟踪和懒加载的前提。
For either of these proxies to be created: 代理类的前提
- A custom data class must be declared with public access.必须是开放(public)申明
- A custom data class must not be sealed (NotInheritable in Visual Basic) 不能是sealed申明
- A custom data class must not be abstract (MustInherit in Visual Basic).不能是抽象类
- A custom data class must have a public or protected constructor that does not have parameters. Use a protected constructor without parameters if you want the CreateObject method to be used to create a proxy for the POCO entity. Calling the CreateObject method does not guarantee the creation of the proxy: the POCO class must follow the other requirements that are described in this topic. 必须有无参的构造函数
- The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces. 不能继承自这两个接口,因为代理类需要使用
- The ProxyCreationEnabled option must be set to true.ProxyCreationEnabled必须开启
For lazy loading proxies:懒加载的前提
- Each navigation property must be declared as public, virtual (Overridable in Visual Basic), and not sealed (NotOverridable in Visual Basic) get accessor. The navigation property defined in the custom data class must have a corresponding navigation property in the conceptual model. For more information, see Loading Related POCO Entities.关联属性必须由virtual标签,懒加载开启
For change tracking proxies:
- Each property that is mapped to a property of an entity type in the data model must have non-sealed (NotOverridable in Visual Basic), public, and virtual (Overridable in Visual Basic) get and set accessors.关联属性必须有virual标签且必须同时拥有get,set
- A navigation property that represents the "many" end of a relationship must return a type that implements ICollection, where T is the type of the object at the other end of the relationship.关联属性在“多”的一端必须申明一个由ICollection标记的属性
- If you want the proxy type to be created along with your object, use the CreateObject method on the ObjectContext when creating a new object, instead of the new operator. 用ObjectContext来新建新对象将默认开启代理类,前提是此对象类型必须满足代理类的前提
如果你的代理类也出现不了,不妨对照上面逐条分析,应该能找到原因。
作者:today4king
出处:https://www.cnblogs.com/jinzhao/archive/2011/12/06/2278077.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2008-12-06 Forms MD5加密的一个细节
2008-12-06 DZM 暴露,记下自己愚蠢的错误