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 hundreds of 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<SalesforceContext> (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:
Formatter [ "Title" ] failed in the evaluation of @page @model WebAppSalesforce.Pages.Contacts.IndexModel @{ ViewData\\["Title"] = "Index"; }
Index
. The error was: Attempt to reference the value of an attribute using an invalid name "Title" @foreach (var item in Model.Contact) { �-
�
}Edit | Delete �-
�