Developer Guide - Entity Framework ASP.NET App with CData ADO.NET Providers

This article provides a guide for using Entity Framework (EF) Core in an ASP.NET Core Razor Pages app. With the help of the CData ADO.NET Provider for CSV, this application will create a page that displays the data in a CSV file as a table.

NOTE: While this article refers to CSV files, the principles can be applied to any of the 270+ data sources CData supports.

Prerequisites 

This guide requires the following parameters: 

Guide

  1. Create the application
    1. Open Visual Studio and create a new “ASP.NET Core Web App (Razor Pages)” project.
    2. Name your project “WebAppASPNet”, so that the namespaces will match if you copy and paste code from this guide, and then select “Next”.
    3. On the next page, select “.NET 8.0 (Long-term support)” and then click “Create”.
  2. Install Entity Framework and add references to the required assemblies
    1. Use the NuGet Package Manager to add the packages listed below to your project:
      1. Microsoft.EntityFrameworkCore
      2. Microsoft.EntityFrameworkCore.Design
      3. Microsoft.EntityFrameworkCore.SqlServer
      4. Microsoft.EntityFrameworkCore.Tools
    2. Add a reference to System.Data.CData.CSV.dll, located in the lib -> net8.0 subfolder in the installation directory.
    3. Add a reference to CData.EntityFrameworkCore.CSV.dll, located in the lib -> net8.0 -> EFCORE80 subfolder in the installation directory.
    4. Build the project to complete the setup for using EF Core.
  3. Add the CSV file (Account.csv) to your project
    1. Right-click on your project in the Solution Explorer then select “Add” > “New Folder”. 
    2. Name the folder “CSVFiles”, then right-click the new folder and select “Add” > “Existing Item…”. 
    3. Add the Account.csv file to your CSVFiles folder. 
  4. Reverse Engineering (Scaffolding) the data model

    Scaffolding is performed using the Package Manager Console (PMC), so open the PMC and use the following command to scaffold all tables and views from the schema into your Models folder. This command automatically constructs classes for the tables/views available. It also creates a context class that extends DbContext and exposes the DbSet properties that represent the tables in the data source: 

    Scaffold-DbContext "URI=YourURI; RowScanDepth=0;UseRowNumbers=True" CData.EntityFrameworkCore.CSV -OutputDir Models -Context CSVContext 
      Notes:
    • Set the “URI” connection property to the folder holding your CSV File, for example: “C:\Users\YourUser\source\repos\WebAppASPNet\WebAppASPNet\CSVFiles\Account.csv". 
    • We set the “RowScanDepth” property to “0” to ensure the entire CSV file is parsed when scanning rows to dynamically determine column names and data types for the tabular representation of the CSV file(s). Scanning the entire document may result in a longer request, but the column data types will be more accurate when reflecting the data model. 
    • Since the CSV file does not have a primary key, we set the UseRowNumbers to “True” which will create a new column with the name “RowNumber” that will be used as a key for the table. Entity Framework relies on primary keys to manage data operations, and any tables/views lacking a primary key will be excluded from the generated model. 

    Read more about the CData CSV ADO.NET Provider connection properties on the CData Documentation page: CData ADO.NET Provider for CSV Documentation 

  5. Add a Razor Page for Accounts
    1. Create a new "Accounts" folder in the “Pages” folder of your project.
    2. In the Solution Explorer, right-click the new “Accounts” folder and select “Add” > “Razor Page…”.
    3. In the Razor Page dialog, select “Razor Page using Entity Framework”.
    4. In the “Razor Page using Entity Framework” dialog:
      1. Set the “Razor Page name” to name your new Razor Page, such as Accounts.
      2. Set Template to “List”. This will create a page that lists all Accounts from the CSV file.
      3. Set “Model class” to the Accounts entity, such as “AccountCSV”.
      4. Set “DbContext class” to the database context, “CSVContext”.
    5. Click “Add” to create the new page. This will create a Razor page in the Pages/Accounts folder: Accounts.cshtml & Accounts.cshtml.cs
  6. Add Connection String in the appsettings.json file

    In the appsettings.json file, you will need to add the connection string for the CSV connection. Below is an example of the code:

    { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": {"CSVContext": "URI=YourURI; RowScanDepth=0;UseRowNumbers=True"} }
  7. Register the context in Program.cs

    In the Program.cs file, you will need to register the context class with the dependency injection container. In the code below, the highlighted section shows the lines added:

    using WebAppASPNet.Models; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddDbContext(options => options.UseCSV(builder.Configuration.GetConnectionString("CSVContext")));
  8. Add the Razor page to your layout

    With the Razor page fully set up, we can remove the generic pages that were available when the project was created and replace them with a link to the new page in the _Layout.cshtml file. To do this, delete the current code and paste the following code into the Pages/Shared/_Layout.cshtml file. The highlighted section is the part that was adjusted from the original:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - WebAppASPNet</title> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> <link rel="stylesheet" href="~/WebAppASPNet.styles.css" asp-append-version="true" /> </head> <body> <header> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <div class="container"> <a class="navbar-brand" asp-area="" asp-page="/Index">WebAppASPNet</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Accounts/Accounts">Accounts</a> </li> </ul> </div> </div> </nav> </header> <div class="container"> <main role="main" class="pb-3"> @RenderBody() </main> </div> <footer class="border-top footer text-muted"> <div class="container"> &copy; 2025 - WebAppASPNet - <a asp-area="" asp-page="/Privacy">Privacy</a> </div> </footer> <script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/js/site.js" asp-append-version="true"></script> @await RenderSectionAsync("Scripts", required: false) </body> </html>
  9. Run the application

    With everything set up, you can now run the application that has a razor page that lists the Accounts from your Accounts.csv file. To do this, first, build the solution then use the green play button to start the application. This should open the home screen for the application, and you can click the “Accounts” tab to view your list of Accounts, as shown below:

Free Community License for data developers

CData ADO.NET Providers further enhance the capabilities of Entity Framework by offering consistent connectivity to more than 270 data sources beyond traditional databases, including SaaS, NoSQL, and big data systems.

With the CData ADO.NET Community License, you get free-forever libraries to access your data in personal .NET projects, all through familiar SQL. Request a license and start building data apps today!