Click here to Skip to main content
13,087,902 members (71,264 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 7 Jun 2012

Unfold POCO with Entity Framework

Rate this:
Please Sign up or sign in to vote.
Unfold POCO with Entity Framework

Plain Old CLR Object or POCO is a simple object to be used with a complicated, special object framework such as an ORM component. In our context, that is POCO with entity framework.

Things to Remember

  1. Deferred/Lazy loading works with POCO entities.
  2. POCO entities are not always needed to be hand-craft. We can use T4 templates for code generation and also entity framework got its own code generation mechanism that is based on T4.
  3. For complex types with POCO cannot use inheritance with your complex type classes
  4. For complex types with POCO only supported  Complex Type is class. Structs are not supported.
  5. If we want the Entity Framework to track changes in POCO classes as the changes occur and support lazy loading of the related objects, we need to use POCO Proxies.
  6. Change tracking with proxies provides better performance compared to snapshot based change tracking.

Simple Workarounds

How to make a POCO deferred enabled?

There are two things you need to do in order to get Deferred Loading support with POCO entities:

context.ContextOptions.DeferredLoadingEnabled = true;
  • Declare the property that you would like to load lazily as virtual. These properties can be any collection type that implements ICollection<T>.
  • Enable deferred loading on the context:

How to Enable Change Tracking Proxies

Just follow these rules when defining the POCO

Class must be public, non-abstract or non-sealed.

Class must also implement public virtual getters/setters for all properties that are persisted.

Declare collection based relationship navigation properties as ICollection<T> only. They cannot be a concrete implementation or another interface that derives from ICollection<T>.

The ProxyCreationEnabled option must be set to true.

context.ContextOptions.ProxyCreationEnabled = true;

Define POCO Entities

This example below defines the User, Contact custom data classes. These classes have been defined to support proxy object creation.

public class User
    public virtual Int32 UserID { get; set; }
    public virtual String First_Name { get; set; }
    public virtual String Last_Name { get; set; }
    public virtual String Email { get; set; }
    public virtual String Password { get; set; }
    public virtual DateTime DOB { get; set; }
    // Defines a navigation property to the Contact object.
    public virtual ICollection<Contact> _Contact { get; set; }
public class Contact
    public virtual Int32 ContactID { get; set; }
    public virtual String Title { get; set; }
    public virtual String Address1 { get; set; }
    public virtual String Address2 { get; set; }
    public virtual String City { get; set; }
    public virtual String State { get; set; }
    public virtual String Zip_Code { get; set; }
    public virtual Int32 Home_Phone { get; set; }
    public virtual String Office_Phone { get; set; }


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shahriar Iqbal Chowdhury/Galib
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Humayun Kabir Mamun29-Jun-13 2:53
memberHumayun Kabir Mamun29-Jun-13 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by Shahriar Iqbal Chowdhury/Galib
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid