Model Context Protocol (MCP) finally gives AI models a way to access the business data needed to make them really useful at work. CData MCP Servers have the depth and performance to make sure AI has access to all of the answers.
Try them now for free →Salesforce ADO.NET Developer Guide - Entity Framework ASP.NET App
This article provides a walkthrough for using Entity Framework (EF) Core in an ASP.NET Core Razor Pages app. With the help of the CData ADO.NET Provider for Salesforce, this application will create pages to view, add, delete, and update Contacts in Salesforce.
NOTE: While this article refers to Salesforce data, the principles can be applied to any of the 270+ data sources CData supports.
Prerequisites
This guide requires the following parameters:
- Visual Studio 2022
- .NET 8.0
- CData ADO.NET Provider for Salesforce
- Access to a Salesforce Account
Guide
-
Create the application
-
Open Visual Studio and create a new “ASP.NET Core Web App (Razor Pages)” project.
-
Name your project “WebAppSalesforce”, so that the namespaces will match if you copy and paste code from this guide, and then select “Next”.
-
On the next page, select “.NET 8.0 (Long-term support)” and then click “Create”.
-
Open Visual Studio and create a new “ASP.NET Core Web App (Razor Pages)” project.
-
Install Entity Framework and add references to the required assemblies
-
Use the NuGet Package Manager to add the packages listed below to your project:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
- Add a reference to System.Data.CData.Salesforce.dll, located in the lib -> net8.0 subfolder in the installation directory (ex. "C:\Program Files\CData\CData ADO.NET Provider for Salesforce 2024\").
- Add a reference to CData.EntityFrameworkCore.Salesforce.dll, located in the lib -> net8.0 -> EFCORE80 subfolder in the installation directory.
- Add the included licensing file (.lic) to your project. You can do this by right-clicking the project in the Solution Explorer and then navigating to Add -> Existing Item -> System.Data.CData.Salesforce.lic (located in the lib -> netstandard2.0 subfolder in your installation directory). Finally, right-click the licensing file, and set the Copy to Output Directory property to Copy if newer.
-
Build the project to complete the setup for using EF Core.
-
Use the NuGet Package Manager to add the packages listed below to your project:
-
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 the schema into your Models folder. This command automatically constructs classes for all 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 "AuthScheme=OAuth; InitiateOAuth=GETANDREFRESH; Tables=Contact;" CData.EntityFrameworkCore.Salesforce -OutputDir Models -Context SalesforceContext
-
Notes:
- We set the Tables property to restrict the number of tables we connect to and model. This will only model the Contact table from the Salesforce connection instead of every available table and view.
- The CData Salesforce ADO.NET Provider supports authentication to Salesforce via either user credentials or the OAuth standard. This example uses the OAuth authentication scheme.
Read more about the CData Salesforce ADO.NET Provider connection properties on the CData Documentation page: CData ADO.NET Provider for Salesforce Documentation
-
Add a Razor Page for the Contacts
- Create a new "Contacts" folder in the “Pages” folder of your project.
- In the Solution Explorer, right-click the new “Contacts” folder and select “Add” > “Razor Page…”.
- In the Razor Page dialog, select “Razor Page using Entity Framework (CRUD)” and click "Add".
-
In the “Razor Page using Entity Framework (CRUD)” dialog:
- Set “Model class” to the Contact entity, "Contact"
- Set “DbContext class” to the database context, “SalesforceContext”.
-
Click “Add” to create the new pages. This will create Razor pages in the Pages/Contacts folder:
-
Add Connection String in the appsettings.json file
In the appsettings.json file, you will need to add the connection string for the Salesforce connection. Below is an example of the code:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": {"SalesforceContext": "AuthScheme=OAuth; InitiateOAuth=GETANDREFRESH; Tables=Contact;" } }
-
Register the context in Program.cs
In the Program.cs file, you will need to register the context class with the dependency injection container. Below is an example of the code:
using WebAppASPNet.Models; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddDbContext
(options => options.UseSalesforce(builder.Configuration.GetConnectionString("SalesforceContext"))); -
Adjust the laout of the Contacts list
For ease of use, we will adjust the layout of the Contacts page so that the controls to Edit or Delete a contact are easily accessible. By default, the column with these controls is listed last, so we will adjust the code in the Index.cshtml file to make this column the first one displayed. In this code, we added an empty header for the column and moved the Edit and Delete controls to the first column listed. Below is an example of the adjustments made:
@page @model WebAppSalesforce.Pages.Contacts.IndexModel @{ ViewData["Title"] = "Index"; }
Index
-
…
Edit | Delete …-
…
-
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. Below is an example of the adjustments made:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - WebAppSalesforce</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="~/WebAppSalesforce.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">WebAppSalesforce</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="/Contacts/Index">Contacts</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"> © 2025 - WebAppSalesforce - <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>
-
Run the application
With everything set up, you can now run the completed Salesforce-connected application. 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 “Contacts” tab to view your list of Contacts, as shown below:
On this same page, select the Create New button to open the page to create a new contact in your Salesforce account:
Additionally, on the same page, select the Edit or Delete buttons next to a contact to open the pages that allow you to edit or delete the contact selected:
Unlock seamless data connectivity in your .NET applications
Are you looking to unlock the full potential of your Salesforce data? CData's Salesforce ADO.NET Provider can seamlessly integrate your .NET applications with your Salesforce data. With the CData ADO.NET Provider for Salesforce 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 Salesforce-connected apps today!