Entity type in EF (EF basics Series 6), ef entity

Source: Internet
Author: User

Entity type in EF (EF basics Series 6), ef entity

EF has two types of entities: POCO Entity and dynamic proxy entity.

POCO Entity (Plain Old CLR Object)

POCO class is a class that does not depend on any base class of the. NET framework. It is like any other common class. This is why it is called "Plain Old CLR Object;

These POCO entities generated by the object data model support most addition, deletion, query, and modification functions. The following is a Studnet POCO entity:

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 proxy class in POCO object running state. It is like a POCO object packaging class. Dynamic packaging entities support lazy loading and automatic tracking of changed features;

A poco entity must meet the following conditions before it can become a POCO proxy.

1. must be defined as Public

2. It cannot be sealed.

3. cannot be abstract;

4. The navigation attribute must be defined as public virtual;

5. Each set property must be Icollection <T>;

6. In the context class, the option ProxyCreationEnabled must not be false (the default value is true );

The following POCO Student entity meets the preceding conditions and will become a dynamic proxy entity during running;

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

Note: The dynamic proxy is valid for each object by default. However, you can manually disable the dynamic proxy by setting ProxyCreationEnabled in the context class to false;

Context. Configuration. ProxyCreationEnabled = false;

When running, the Student POCO object will become a proxy class:

The proxy class has two attributes: a scalar attribute and a navigation attribute,

The actual values of a scalar property are included in the object. For example, the Student object has the scalar property StudentID and StudentName, which are consistent with the fields in the database;

The navigation attribute points to another associated object. The Student object has a Standard attribute as the navigation attribute, which ensures that the program can navigate from a Student to the associated standard object.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.