EF6 Working with Proxies ProxyCreationEnabled, workingwith

Source: Internet
Author: User

EF6 Working with Proxies ProxyCreationEnabled, workingwith
When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. this proxy overrides some virtual properties of the entity to insert hooks for inserting actions automatically when the property is accessed. the proxy class overwrites the virtual property For example, which is used to support lazy loading of relationships. the techniques shown in this topic apply equally to models created with Code First and the EF Designer. in lazy loading, Most of the time you don't need to be aware of this use of proxies, but there are exceptions: the proxy class cannot be used during serialization. in some scenarios you might want to prevent the Entity Framework from creating proxy instances. for example, when you're serializing entities you generally want the POCO classes, not the proxy classes. one way to avoid serialization problems is to serialize data transfer objects (DTOs) instead of entity objects, as shown in the Using Web API with Entity Framework tutorial. another way is to disable proxy creation.2. When you instantiate an entity class using the new operator, you don't get a proxy instance. this means you don't get functionality such as lazy loading and automatic change tracking. this is typically okay; you generally don't need lazy loading, because you're creating a new entity that isn' t in the database, and you generally don't need change tracking if you're explicitly marking the entity as Added. however, if you do need lazy loading and you need change tracking, you can create new entity instances with proxies using the Create method of the DbSet class.3. You might want to get an actual entity type from a proxy type. you can use the GetObjectType method of the ObjectContext class to get the actual entity type of a proxy type instance.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.