Saturday 12 July 2014

Type Of Entity

There are four types of Entities in Entity Framework 4.x, 1) EntityObject 2) POCO 3) POCO Proxy 4) Self-Tracking Entities.

EntityObject: By default, the ADO.NET Entity Data Model tools generate EntityObject derived entities. If you check entities created in previous step, it is of this type. When you work with EntityObject derived types, the object context manages the relationships between your objects, tracks changes as they occur, and supports lazy loading in the most efficient manner. However, the EntityObject derived types have strong dependency on the Entity Framework. If you are working with architectures that require persistence ignorance (for example, test- driven development or domain-driven development) or you have existing domain classes, consider using POCO or POCO proxies.
Following is an example of auto generated Student entity which is derived from EntityObject class:

[EdmEntityTypeAttribute(NamespaceName="SchoolDBModel", Name="Student")]
   [Serializable()]
   [DataContractAttribute(IsReference=true)]
   public partial class Student : EntityObject
   {
       #region Factory Method
   
       /// <summary>
       /// Create a new Student object.
       /// </summary>
       /// <param name="studentID">Initial value of the StudentID property.</param>
       /// <param name="standardId">Initial value of the StandardId property.</param>
       public static Student CreateStudent(global::System.Int32 studentID, global::System.Int32 standardId)
       {
           Student student = new Student();
           student.StudentID = studentID;
           student.StandardId = standardId;
           return student;
       }

       #endregion
       #region Primitive Properties
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
       [DataMemberAttribute()]
       public global::System.Int32 StudentID
       {
           get
           {
               return _StudentID;
           }
           set
           {
               if (_StudentID != value)
               {
                   OnStudentIDChanging(value);
                   ReportPropertyChanging("StudentID");
                   _StudentID = StructuralObject.SetValidValue(value);
                   ReportPropertyChanged("StudentID");
                   OnStudentIDChanged();
               }
           }
       }
       private global::System.Int32 _StudentID;
       partial void OnStudentIDChanging(global::System.Int32 value);
       partial void OnStudentIDChanged();
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
       [DataMemberAttribute()]
       public global::System.String StudentName
       {
           get
           {
               return _StudentName;
           }
           set
           {
               OnStudentNameChanging(value);
               ReportPropertyChanging("StudentName");
               _StudentName = StructuralObject.SetValidValue(value, true);
               ReportPropertyChanged("StudentName");
               OnStudentNameChanged();
           }
       }
       private global::System.String _StudentName;
       partial void OnStudentNameChanging(global::System.String value);
       partial void OnStudentNameChanged();
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
       [DataMemberAttribute()]
       public global::System.Int32 StandardId
       {
           get
           {
               return _StandardId;
           }
           set
           {
               OnStandardIdChanging(value);
               ReportPropertyChanging("StandardId");
               _StandardId = StructuralObject.SetValidValue(value);
               ReportPropertyChanged("StandardId");
               OnStandardIdChanged();
           }
       }
       private global::System.Int32 _StandardId;
       partial void OnStandardIdChanging(global::System.Int32 value);
       partial void OnStandardIdChanged();

       #endregion
   
       #region Navigation Properties
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [XmlIgnoreAttribute()]
       [SoapIgnoreAttribute()]
       [DataMemberAttribute()]
       [EdmRelationshipNavigationPropertyAttribute("SchoolDBModel", "FK_Student_Standard", "Standard")]
       public Standard Standard
       {
           get
           {
               return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Standard>("SchoolDBModel.FK_Student_Standard", "Standard").Value;
           }
           set
           {
               ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Standard>("SchoolDBModel.FK_Student_Standard", "Standard").Value = value;
           }
       }
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [BrowsableAttribute(false)]
       [DataMemberAttribute()]
       public EntityReference<Standard> StandardReference
       {
           get
           {
               return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Standard>("SchoolDBModel.FK_Student_Standard", "Standard");
           }
           set
           {
               if ((value != null))
               {
                   ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Standard>("SchoolDBModel.FK_Student_Standard", "Standard", value);
               }
           }
       }
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [XmlIgnoreAttribute()]
       [SoapIgnoreAttribute()]
       [DataMemberAttribute()]
       [EdmRelationshipNavigationPropertyAttribute("SchoolDBModel", "FK_StudentAddress_Student", "StudentAddress")]
       public StudentAddress StudentAddress
       {
           get
           {
               return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<StudentAddress>("SchoolDBModel.FK_StudentAddress_Student", "StudentAddress").Value;
           }
           set
           {
               ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<StudentAddress>("SchoolDBModel.FK_StudentAddress_Student", "StudentAddress").Value = value;
           }
       }
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [BrowsableAttribute(false)]
       [DataMemberAttribute()]
       public EntityReference<StudentAddress> StudentAddressReference
       {
           get
           {
               return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<StudentAddress>("SchoolDBModel.FK_StudentAddress_Student", "StudentAddress");
           }
           set
           {
               if ((value != null))
               {
                   ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<StudentAddress>("SchoolDBModel.FK_StudentAddress_Student", "StudentAddress", value);
               }
           }
       }
   
       /// <summary>
       /// No Metadata Documentation available.
       /// </summary>
       [XmlIgnoreAttribute()]
       [SoapIgnoreAttribute()]
       [DataMemberAttribute()]
       [EdmRelationshipNavigationPropertyAttribute("SchoolDBModel", "StudentCourse", "Course")]
       public EntityCollection<Course> Courses
       {
           get
           {
               return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Course>("SchoolDBModel.StudentCourse", "Course");
           }
           set
           {
               if ((value != null))
               {
                   ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Course>("SchoolDBModel.StudentCourse", "Course", value);
               }
           }
       }

       #endregion
   }


POCO (Plain Old CLR Object): POCO class is the class which doesn’t depend on any framework specific base class. It is like any other normal .net class that is why it is called “Plain Old CLR Objects”. The Entity Framework 4.1 enables you to use existing .net classes together with the data model without making any modifications to the existing .net classes. These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as EntityObject derived entities .
Following is a Student POCO class:

public class Student
   {
       public int StudentID { get; set; }

       public string StudentName{ get; set; }

       public Standard Standard{ get ; set; }

       public StudentAddress StudentAddress{ get ; set; }

       public IList<Course> Courses{ get; set; }

   }

POCO Proxy: POCO Proxy is a runtime proxy class of POCO entity. POCO entity becomes POCO Proxy entity if it meets certain requirements to enable lazy loading proxy and instant change tracking. It adds some methods at runtime to your POCO class which does instant change tracking and lazy loading stuff.
POCO entity should meet the following requirement to become POCO proxy:
  1. A custom data class must be declared with public access.
  2. A custom data class must not be sealed (NotInheritable in Visual Basic)
  3. A custom data class must not be abstract (MustInherit in Visual Basic).
  4. A custom data class must have a public or protected constructor that does not have parameters.
  5. The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.
  6. The ProxyCreationEnabled option must be set to true.
  7. Each navigation property must be declared as public, virtual

public class Student
   {
       public virtual int StudentID { get; set; }

       public virtual string StudentName { get; set; }

       public virtual Standard Standard { get; set; }

       public virtual StudentAddress StudentAddress { get; set; }

       public virtual IList<Course> Courses { get; set; }

   }

Self-Tracking Entities: The EntityObject derived entities, POCO, and POCO proxy entities works well in 

No comments:

Post a Comment