Defining Data Model Classes

Description Article
Table of Contents
Summary

The Data Model is defined by a set of POCO classes where each of these classes represents a data table in the relational database and each oproxybject instance represents a row within the database table.

These classes are referred to as “Entity Classes“.

Description

These POCO classes that represent the entity classes are manually created when using the Code First model approach.

If Model First or Database First approaches are used instead, the POCO classes can be generated from the EDM model file on Visual Studio:

  1. Open the “.edmx” file in the Entity Data Model Designer;
  2. Select the “Add Code generation Item” on the model; and
  3. Select the  DbContext Generator template

The classes generated will include a POCO class for each entity class and a derived class from DbContext.

Operations
Simple entity class

An entity class, as any POCO class, is a class that doesn’t depend on any framework specific base class and is a persistence-ignorant object.

These POCO entities support query, insert, update, and delete behaviors as entity types are generated by the Entity Data Model (Database First or Model First approaches) or written by the developer (Code First approach).

How to do it
public class Student
{
	public Student()
	{
		this.Courses = new List<Course>();
	}

	public int StudentID { get; set; }
	public string StudentName { get; set; }
	public Nullable<int> StandardId { get; set; }

	public Standard Standard { get; set; }
	public StudentAddress StudentAddress { get; set; }
	public IList<Course> Courses { get; set; }
}
Dynamic Proxy (POCO Proxy)

Dynamic Proxy is a runtime generated proxy class of an POCO entity.

It works like a wrapper class of POCO entity. Dynamic proxy entities allow lazy loading and automatic change tracking.

At runtime the type of an entity proxy will be derivated from System.Data.Entity.DynamicProxies.

Remarks

A POCO entity should meet the following requirements to become a POCO proxy:

  • The POCO class must be declared with public access
  • The POCO class must NOT be sealed (NotInheritable in Visual Basic)
  • The POCO class must NOT be abstract (MustInherit in Visual Basic)
  • Each navigation property MUST be declared as public and virtual
  • Each collection property MUST be of type ICollection<T>

The configuration of the context class must follow the following:

  • The DbContext.ProxyCreationEnabled option must NOT be false (default is true)
  • The DbContext.ProxyCreationEnabled option must NOT be false (default is true)

How to do it

The following Student POCO entity meets all the above requirement to become dynamic proxy entity at runtime:

public class Student
{
	public Student()
	{
		this.Courses = new HashSet<Course>();
	}

	public int StudentID { get; set; }
	public string StudentName { get; set; }
	public Nullable<int> StandardId { get; set; }

	public virtual Standard Standard { get; set; }
	public virtual StudentAddress StudentAddress { get; set; }
	public virtual ICollection<Course> Courses { get; set; }
}

 At runtime a proxy created for the Student entity class will be of type System.Data.Entity.DynamicProxies.Student as below:

 The actual entity type from a dynamic proxy can be obtained using the ObjectContext.GetObjectType() method: