Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Unfold POCO with Entity Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
7 Jun 2012CPOL1 min read 14.3K   7   1
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:

C#
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.

C#
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.

C#
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; }
}

License

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


Written By
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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun29-Jun-13 2:53
Humayun 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.