In this article I will explain with an example, what is ORM and how to use in ASP.Net using C# and VB.Net.
 
 

What is ORM?

1. ORM stands for Object-Relational-Mapping, it's used in software development to connect and work with databases using an object-oriented programming language, like C# and Java.
2. An ORM library encapsulates the code needed to manipulate the data, thus removing the use of SQL. One can directly interact with an objects rather than SQL queries, improving code readability and maintainability.
3. It maps database Tables to Classes, Rows and Columns to object properties.
4. Repetitive SQL code is reduced, enabling faster application development.
5. Database independence is achieved, thus facilitating easier migration across different database systems.
6. It centralizes data access logic, reducing the risk of errors and SQL injection attacks.
7. Entity Framework Core is a full-featured ORM framework.
 
 

Database

Here I am making use of Microsoft�s Northwind Database. You can download it from here.
 
 

Configuring and connecting Entity Framework to database

1. Inside the Solution Explorer, right click on Project and then click on Add and then click on Add New Item.
What is ORM and How to use in ASP.Net
 
2. Then, inside the Add New Item window, select ADO.NET Entity Data Model option.
What is ORM and How to use in ASP.Net
 
3. As soon as you add the Entity Data Model to your project you will be prompted with the following dialog and you need to click YES button.
What is ORM and How to use in ASP.Net
 
4. Then, the Entity Data Model Wizard will open up where you need to select EF Designer from database.
What is ORM and How to use in ASP.Net
 
5. After that, click on New Connection Button.
What is ORM and How to use in ASP.Net
 
6. Now the wizard will ask you to connect and configure the connection string to the database.
What is ORM and How to use in ASP.Net
 
After connecting to the database, click on Test Connection and if connection was successful it will display the Success message.
What is ORM and How to use in ASP.Net
 
7. Then, click on Next Button to move on to the Next step.
What is ORM and How to use in ASP.Net
 
8. Now, you need to select the Table you need to connect and work with Entity Framework and click on Finish Button.
What is ORM and How to use in ASP.Net
 
Finally, the Entity Data Model ready with the Customers Table of the Northwind database.
What is ORM and How to use in ASP.Net
 
 

HTML Markup

The HTML Markup consists of following control.
GridView – For displaying data.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="Customer Id" />
        <asp:BoundField DataField="ContactName" HeaderText="CustomerName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
    </Columns>
</asp:GridView>
 
 

Binding GridView with Entity Framework using C# and VB.Net

Inside the Page_Load event handler, the Top 10 records from the Customers table are fetched using Entity Framework and assigned to the DataSource property of the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        using (NORTHWINDEntities entities = new NORTHWINDEntities())
        {
            gvCustomers.DataSource = (from customer in entities.Customers.Take(10)
                                      select customer).ToList();
            gvCustomers.DataBind();
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Using entities As NORTHWINDEntities = New NORTHWINDEntities()
            gvCustomers.DataSource = (From customer In entities.Customers.Take(10) Select customer).ToList()
            gvCustomers.DataBind()
        End Using
    End If
End Sub
 
 

Screenshot

What is ORM and How to use in ASP.Net
 
 

Downloads



Other available versions