In this article I will explain with an example, how to populate (bind) RDLC Report using Entity Framework as Data Source in ASP.Net MVC Razor.
This article will illustrate how to configure Entity Framework and connect to SQL Server database and then finally use the Entity Framework data as Data Source to populate RDLC Reports in ASP.Net MVC Razor.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Creating an Entity Data Model
The very first step is to create an ASP.Net MVC Application and connect it to the Northwind Database using Entity Framework. For more details please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example.
Following is the Entity Data Model of the Customers Table of the Northwind Database which will be used later in this project.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
 
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Top 10 Customer records are fetched and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(from customer in entities.Customers.Take(10)
                    select customer);
    }
}
 
 
View
Now you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
The Name of the View is set to Index, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework) and finally the Data context class is set to NorthwindEntities.
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
There is an HTML Anchor Link which redirects the User to the Reports.aspx (Web Form page discussed later) used for displaying the RDLC Report.
@model IEnumerable<RDLC_Reports_MVC.Customer>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <h4>Customers</h4>
    <hr/>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerID</th>
            <th>ContactName</th>
            <th>City</th>
            <th>Country</th>
        </tr>
        @foreach (Customer customer in Model)
        {
            <tr>
                <td>@customer.CustomerID</td>
                <td>@customer.ContactName</td>
                <td>@customer.City</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
    <br/>
    <a href="~/Reports/Report.aspx">View Report</a>
</body>
</html>
 
 
Configuring RDLC Report in ASP.Net MVC
1. The very first step is to right click the Project in the Solution Explorer and click Add and then New Item and then select Report Wizard and set a name for the RDLC Report.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
2. Once you click Add button in the above dialog, the following dialog appears where you will need to create a DataSet for the RDLC Report.
First you need to set a Name for the DataSet and then you need to click New button in order to configure the Data Source.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
Then you will need to select the NorthwindConnectionString and click Next button.
Note: The NorthwindConnectionString is already present in the Web.Config file as it is used by the Entity Framework.
 
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
Now you need to choose the Table(s) which will be used to populate the DataSet for the RDLC Report and then click Finish button.
Note: The Tables selected here must be same as the Tables selected while configuring the Entity Framework.
 
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
The DataSet is now configured and now we can proceed further by clicking the Next button.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
3. In this dialog, you will need to choose the Fields to be displayed in the RDLC report. This can be done by simple drag and drop of Field from Available fields box to the Values box.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
4. This dialog will ask to choose the Layout. It is not needed here and can be skipped as we are not performing any calculations.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
5. Finally we need to choose the style, i.e. color and theme of the Report.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
Once you click Finish button, your RDLC Report should look as below.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
6. RDLC Report works only with a RDLC Report Viewer control which is available only in ASP.Net Web Forms and hence for displaying a RDLC Report, you will need to add a Web Forms page.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
7. Now in the ASP.Net Web Forms page, you will need to add a RDLC Report Viewer control from the ToolBox.
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
 
HTML Markup
The HTML Markup consists of an ASP.Net AJAX Script Manager control and ASP.Net Report Viewer control.
<%@Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="RDLC_Reports_MVC.Reports.Report" %>
 
<%@Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
 
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false"></rsweb:ReportViewer>
    </form>
</body>
</html>
 
 
Namespaces
You will need to import the following namespace.
using Microsoft.Reporting.WebForms;
 
 
Populating the RDLC Report using Entity Framework DataSource
Inside the Page Load event, the RDLC Report is populated with the records of the database using Entity Framework.
public partial class Report : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
        NorthwindEntities entities = new NorthwindEntities();
        ReportDataSource datasource = new ReportDataSource("Customers", (from customer in entities.Customers.Take(10)
                                                                         select customer));
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}
 
 
Screenshots
HTML Grid displaying Entity Framework records
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
RDLC Report displaying Entity Framework records
Populate (Bind) RDLC Report using Entity Framework DataSource in ASP.Net MVC
 
 
Downloads