We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →How to Access Salesforce Data Cloud Data Using Entity Framework
This article shows how to access Salesforce Data Cloud data using an Entity Framework code-first approach. Entity Framework 6 is available in .NET 4.5 and above.
Microsoft Entity Framework serves as an object-relational mapping framework for working with data represented as objects. Although Visual Studio offers the ADO.NET Entity Data Model wizard to automatically generate the Entity Model, this model-first approach may present challenges when your data source undergoes changes or when you require greater control over entity operations. In this article, we will delve into the code-first approach for accessing Salesforce Data Cloud data through the CData ADO.NET Provider, providing you with more flexibility and control.
- Open Visual Studio and create a new Windows Form Application. This article uses a C# project with .NET 4.5.
- Run the command 'Install-Package EntityFramework' in the Package Manger Console in Visual Studio to install the latest release of Entity Framework.
Modify the App.config file in the project to add a reference to the Salesforce Data Cloud Entity Framework 6 assembly and the connection string.
Salesforce Data Cloud supports authentication via the OAuth standard.
OAuth
Set AuthScheme to OAuth.
Desktop Applications
CData provides an embedded OAuth application that simplifies authentication at the desktop.
You can also authenticate from the desktop via a custom OAuth application, which you configure and register at the Salesforce Data Cloud console. For further information, see Creating a Custom OAuth App in the Help documentation.
Before you connect, set these properties:
- InitiateOAuth: GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthClientId (custom applications only): The Client ID assigned when you registered your custom OAuth application.
- OAuthClientSecret (custom applications only): The Client Secret assigned when you registered your custom OAuth application.
When you connect, the driver opens Salesforce Data Cloud's OAuth endpoint in your default browser. Log in and grant permissions to the application.
The driver then completes the OAuth process as follows:
- Extracts the access token from the callback URL.
- Obtains a new access token when the old one expires.
- Saves OAuth values in OAuthSettingsLocation so that they persist across connections.
For other OAuth methods, including Web Applications and Headless Machines, refer to the Help documentation.
<configuration> ... <connectionStrings> <add name="SalesforceDataCloudContext" connectionString="Offline=False;InitiateOAuth=GETANDREFRESH" providerName="System.Data.CData.SalesforceDataCloud" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.SalesforceDataCloud" type="System.Data.CData.SalesforceDataCloud.SalesforceDataCloudProviderServices, System.Data.CData.SalesforceDataCloud.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
- Add a reference to System.Data.CData.SalesforceDataCloud.Entities.EF6.dll, located in the lib -> 4.0 subfolder in the installation directory.
- Build the project at this point to ensure everything is working correctly. Once that's done, you can start coding using Entity Framework.
- Add a new .cs file to the project and add a class to it. This will be your database context, and it will extend the DbContext class. In the example, this class is named SalesforceDataCloudContext. The following code example overrides the OnModelCreating method to make the following changes:
- Remove PluralizingTableNameConvention from the ModelBuilder Conventions.
- Remove requests to the MigrationHistory table.
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class SalesforceDataCloudContext : DbContext { public SalesforceDataCloudContext() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<SalesforceDataCloudContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
- Create another .cs file and name it after the Salesforce Data Cloud entity you are retrieving, for example, Account. In this file, define both the Entity and the Entity Configuration, which will resemble the example below:
using System.Data.Entity.ModelConfiguration; using System.ComponentModel.DataAnnotations.Schema; [System.ComponentModel.DataAnnotations.Schema.Table("Account")] public class Account { [System.ComponentModel.DataAnnotations.Key] public System.String [Account ID] { get; set; } public System.String [Account Name] { get; set; } }
- Now that you have created an entity, add the entity to your context class:
public DbSet<Account> Account { set; get; }
- With the context and entity finished, you are now ready to query the data in a separate class. For example:
SalesforceDataCloudContext context = new SalesforceDataCloudContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Account select line;