Skip to main content

POCO Classes in Entity FrameWork

A Plain Old CLR Objects (POCO) is a class, which doesn't depend on any framework-specific base class. It is like any other normal .NET class. Due to this, they are called Plain Old CLR Objects. These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as Entity Object derived entities. These classes (POCO classes) implements only the domain business logic of the Application.

Some developers use Data Transfer Objects (DTOs) with the classes to pass the data between the layers because POCOs are also used to pass the data between the layers, but they become heavy. Hence they use DTOs, which are also theclasses.

The main difference between DTO and POCO is that DTOs do not contain any methods. They only contain public members. Thus, sending the data, using a DTO is easy because they are lightweight objects.
 
The code given below defines a POCO class.


If you want to create POCO classes instead of the entity classes or a default entity object, then you can create POCO entity classes.

To create POCO classes, we first need to disable auto create classes or auto create code generation, which generates Context classes entity code in Model1.designer.cs. To disable this code generation, right-click on model1.edmx (ADO.NET data modal) and click property Now, you will see the value of "Custom Tool" as EntityModelCodeGenerator and you need to remove this value.



After removing the value Custom tool, you will see that in modal1.edmx, there is no Model1.designer class. Now, we must create properties (Context and Entities), so for this, we need to create POCOs classes.

Now, double-click on Modal1.edmx and right-click on the designer surface and click on the code generation Items. A screen will open from where you need to select ADO.NET POCO Entity Generator and click Add.


After clickinig Add button, you will see 2 classes, where one is modal1.context.tt and another is modal1.tt.



Model1.Context.tt is a context file and Model1.tt is an entity file. You can modify this file, if you want to generate your template. The Model1.Context.cs file has a context class and .cs files under Model1.tt are the entity classes.

Entity classes have all the properties as Virtual. In other words, these entities fulfill the requirements of POCO Proxy entities. These entities can be used as POCO entities or POCO Proxy entities. By default, it will behave as POCO Proxy entities, but you can disable proxy creation by setting a property "ObjectContext.ContextOptions.ProxyCreationEnabled = false".

Note
  • If you want to write a unit test for the context, then replace ObjectSet<> to IObjectSet<>.
  • If you are not able to see ADO.NET POCO Entity Generator, then you must install NuGet Package Library.

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome...