Entity Framework tutorial -- Entity type in Entity Framework, entityframework
Entity types in Entity Framework:
In the previous chapter, we introduced the creation of EDM from an existing database, which contains the entities corresponding to each table in the database. There are POCO and dynamic proxy entities in EF 5.0/6.0.
POCO Entity (Plain Old CLR Object ):
The POCO class does not depend on any framework type. It is called "Plain Old CLR Objects", just like other normal types. (I don't know how to translate them here. A common CLR object? Old CLR object? It probably means there are no special objects ).
POCO objects (also known as non-persistent objects) are Entity types created by EDM that support queries, inserts, updates, and deletes. Below is a Student's POCO entity
1 public class Student 2 { 3 public Student() 4 { 5 this.Courses = new List<Course>(); 6 } 7 8 public int StudentID { get; set; } 9 public string StudentName { get; set; }10 public Nullable<int> StandardId { get; set; }11 12 public Standard Standard { get; set; }13 public StudentAddress StudentAddress { get; set; }14 public IList<Course> Courses { get; set; }15 }16
View Code
Dynamic proxy (POCO proxy ):
Dynamic proxy is the runtime principle of POCO entities. It looks like the encapsulation class of POCO entities first. Dynamic proxy entities allow lazy loading and automatic change tracking.
The POCO entity meets the following conditions:
The following Student POCO entity satisfies all the preceding conditions, so it will become a dynamic proxy entity.
1 public class Student 2 { 3 public Student() 4 { 5 this.Courses = new HashSet<Course>(); 6 } 7 8 public int StudentID { get; set; } 9 public string StudentName { get; set; }10 public Nullable<int> StandardId { get; set; }11 12 public virtual Standard Standard { get; set; }13 public virtual StudentAddress StudentAddress { get; set; }14 public virtual ICollection<Course> Courses { get; set; }15 }
View Code
Note:By default, all objects allow dynamic proxy. Of course, you can also disable it In the context class.
Context. Configuration. ProxyCreationEnabled = false;
EDM creates such proxy entities by default.
The Student type will change to System. Data. Entity. DynamicProxies. Student at runtime.
Obtain the actual type from the dynamic Proxy:
You can use ObjectContext. GetObjectType () to obtain the actual type from the dynamic proxy.
An object has two types of attributes: Scalar attribute and navigation attribute.
Scalar property:
A scalar attribute is the actual value contained in an object. For example, the attributes of StudentId and StudentName in the Student object. These correspond to columns in the Student table.
Navigation properties:
Navigation properties point to other related entities. The Student class has a Standard navigation attribute, which enables applications to relate to the corresponding Standard object through Student.