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博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2011/12/06/2278077.html,如需转载请自行联系原作者

相关文章
|
5月前
|
Java
Java反射的详细解析之三
面试题: 你觉得反射好不好?好,有两个方向 第一个方向:无视修饰符访问类中的内容。但是这种操作在开发中一般不用,都是框架底层来用的。 第二个方向:反射可以跟配置文件结合起来使用,动态的创建对象,动态的调用方法。
21 0
|
6月前
ABAP None-Class-Based 异常处理的一些局限性介绍试读版
ABAP None-Class-Based 异常处理的一些局限性介绍试读版
17 0
|
Java API
JavaSE:第八章:java常用类
JavaSE:第八章:java常用类
JavaSE:第八章:java常用类
|
设计模式 Java
JavaSE:第七章:高级类特性
JavaSE:第七章:高级类特性
JavaSE:第七章:高级类特性
JavaSE:第十三章:java反射机制
JavaSE:第十三章:java反射机制
JavaSE:第十三章:java反射机制
|
Java 程序员 开发工具
带你读《Effective Java中文版》之三:对于所有对象都通用的方法
本书旨在帮助读者更加有效地使用Java编程语言及其基本类库java.lang、java.util和java.io,以及子包java.util.concurrent和java.util.function等全书以一种比较松散的方式将这些条目规则组织成12章,每一章都涉及软件设计的一个主要方面。
|
Java API 程序员
带你读《Effective Java中文版》之二:创建和销毁对象
本书旨在帮助读者更加有效地使用Java编程语言及其基本类库java.lang、java.util和java.io,以及子包java.util.concurrent和java.util.function等全书以一种比较松散的方式将这些条目规则组织成12章,每一章都涉及软件设计的一个主要方面。
|
物联网 程序员 .NET