Connect C# to Zoho CRM With Entity Framework Core

This guide walks you through using Entity Framework Core to manage data operations in .NET applications. With the help of dotConnect for Zoho CRM, you'll learn how to generate EF Core models using the visual ORM tool, Entity Developer. We'll also build a console application and demonstrate how to implement CRUD operations efficiently using a well-structured connection class.

Why dotConnect for Zoho CRM?

dotConnect for Zoho CRM is a powerful data provider designed for seamless interaction with Zoho services. It enables the creation of a dynamic connector with flexible configuration options, making integration smooth and trouble-free. Being fully compatible with Visual Studio, it streamlines development and enhances ORM capabilities, providing robust support for Entity Framework Core and other ORM tools.

Prerequisites

  • Visual Studio 2022: Our IDE of choice. If you don't have it on your computer, go to the official website to download and install it. We'll use the Community edition, so you can get it too.
  • dotConnect for Zoho CRM: A feature-rich ADO.NET provider with support for Entity Framework, NHibernate, and EF Core. For installation instructions, refer to the next section.
  • Entity Developer: An ORM designer for .NET ORM frameworks with powerful code generation. For now, just download the installer.

Download and activate dotConnect for Zoho CRM

30-day free trial version

Download and install dotConnect for Zoho CRM directly on your computer, or install the Devart.Data.Zoho NuGet package.

No license key is required; you can start exploring the product immediately.

Full version

After purchasing the full version, go to your profile's Licenses page. Choose your product and click Details. Here, you'll find the license details and the Activation Key.

The Licenses page displays license details and the Activation Key

To activate a connection in your application, add the License Key to your connection string.

Create a .NET Core project

1. Open Visual Studio, select Create a new project > Console App (.NET Core), and click Next.

2. Name your project, for example, ZohoEFCore, specify its location, and click Next.

3. To create the project, click Create.

4. In the Solution Explorer, right-click the created project and select Manage NuGet Packages.

5. On the Browse tab, search for the Devart.Data.Zoho.EFCore and Microsoft.EntityFrameworkCore packages and install them.

Check Zoho CRM objects

To connect to Zoho CRM using the built-in Data Explorer, click Tools and select Connect to Database.

Establish a new connection from the menu

Then, select the Zoho CRM data source, enter your Zoho domain, and click Web Login.

Add a connection

Log in to your Zoho account and grant access to the Zoho CRM company.

Grant access to the Zoho CRM company

After that, select the tables and fields you want to work with and retrieve the data.

Select the tables and fields to work with

Build a Zoho CRM EF Core model

The Professional and Developer editions of dotConnect for Zoho CRM come equipped with Entity Developer, a robust ORM model designer and code generator that supports both Entity Framework and EF Core. This tool simplifies the process of generating a model from a data source or creating a data source from an existing model.

1. Add Devart EF Core Model to the project. To do this, right-click the project node in Solution Explorer, point to Add, and click New Item.

Add Devart EF Core Model

2. In the Add New Item dialog, select the Data category, select the Devart EF Core Model template, and click Add. This automatically launches Entity Developer's Create New Model wizard, which creates a new empty model or generates it from the data source.

The Add New Item dialog

3. Select the Database First approach and click Next on the welcome screen.

Select the Database First approach

4. Configure your Zoho CRM connection:

  • Specify the API version (v2 or v4).
  • Enter your Zoho domain.
  • Click Web Login to obtain a Refresh Token, similarly to the way you establish a connection in Data Explorer.

Once you have successfully connected, click OK and Next.

Configure your Zoho CRM connection

5. Select Zoho CRM objects for your model. In this example, we will use Account fields: AccountName, Phone, Website, and AccountType. Click Next.

Select Zoho CRM objects for your model

6. Set up property naming conventions for data source objects. We recommend using the default settings for this tutorial. Click Next.

Set up naming conventions

7. Input ZohoEFCore as namespace, and ZohoModel as the name of the DataContext descendant. This will be the name of the main data access class. Click Next.

Configure model properties

8. Define the parameters for object generation. For this tutorial, we'll use the default settings. Click Next.

Choose code generation templates

Click Finish. The model will be generated and opened in Entity Developer.

Finalize the model

The model you just created is now ready for use.

  • Entity Developer generates classes for the selected tables, representing data entities.
  • It also creates a ZohoModel class descendant, which manages the connection to the Zoho CRM database and handles the data flow.
  • This class contains properties and methods corresponding to your Zoho CRM objects, allowing you to retrieve and modify data easily.

The generated code can be found in:

  • DataModel1.Account.cs – Contains entity definitions.
  • DataModel1.ZohoModel.cs – You can extend functionality by writing custom partial classes and methods here.

With the model set up, you are now ready to interact with Zoho CRM data using Entity Framework Core in your .NET application.

Note
If you're using a purchased license for dotConnect for Zoho CRM, include the License Key in the connection string. In our case, we will keep it in the DbContext class: optionsBuilder.UseZoho(@"Domain=crm.zoho.com;API Version=v2;Client Id=*****;Client Secret=*****;Refresh Token=*****;License Key=*****");

Read data from Zoho CRM

In the Program.cs file, write the code to retrieve and display information from the Account table.

static void DisplayAccounts()
{
    using (var context = new ZohoModel())
    {
        var accounts = context.Accounts
            .Select(a => new { a.AccountName, a.Phone, a.Website, a.AccountType })
            .ToList();

        foreach (var account in accounts)
        {
            Console.WriteLine($"Account Name: {account.AccountName}");
            Console.WriteLine($"Phone: {account.Phone}");
            Console.WriteLine($"Website: {account.Website}");
            Console.WriteLine($"Account Type: {account.AccountType}");
            Console.WriteLine(new string('-', 20));
        }
    }
}

The console should display the AccountName, Phone, Website, and AccountType fields from the Account table in Zoho CRM.

Read data from Zoho CRM

Insert new data into Zoho CRM

This example demonstrates how to insert a new account into the Zoho CRM database using Entity Framework Core.

First, ensure that your Account class includes an Id field. To do this, right-click your model in Model Explorer and select Update Model from Database.

Update the data model

Next, select the newly added Id column and update the data model.

Select the Id column

After updating, verify the properties of the Id field. Ensure that:

  • Primary Key is checked.
  • Value Generated is set to OnAdd.
Verify the properties of the Id field

Once these settings are configured, the model is ready for inserting new records into Zoho CRM.

Create a method in your Program class to add a new account to the Account table.

static void AddNewAccount()
{
	using (var context = new ZohoModel())
	{
    	var newAccount = new Account
    	{
        	AccountName = "New Company Inc.",
        	Phone = "123-456-7890",
        	Website = "http://www.newcompany.com",
        	AccountType = "Customer"
    	};

    	context.Accounts.Add(newAccount);
    	context.SaveChanges();

    	Console.WriteLine("New account added successfully.");
	}
}

Build and run your application.

Build and run your application

Update Zoho CRM data

Now, let's update the phone number for an account with the name "Tom Ford" in Zoho CRM. This example assumes that the AccountName is unique or that you have a way to uniquely identify the account you want to update.

We retrieve the account named "Tom Ford" from the database and update the Phone property with a new phone number.

Here's how you can implement this in your Program class:

static void Main(string[] args)
{
	UpdateAccountPhoneNumber("Tom Ford", "222-333-4444");
	DisplayAccounts();
}

static void UpdateAccountPhoneNumber(string accountName, string newPhoneNumber)
{
	using (var context = new ZohoModel())
	{
    	// Fetch the account with the specified name
    	var account = context.Accounts
        	.FirstOrDefault(a => a.AccountName == accountName);

    	if (account != null)
    	{
        	// Update the phone number
        	account.Phone = newPhoneNumber;

        	// Save changes to the database
        	context.SaveChanges();

        	Console.WriteLine($"Phone number for {accountName} updated successfully.");
    	}
    	else
    	{
        	Console.WriteLine($"Account with name {accountName} not found.");
    	}
	}
}

Here is the result.

Update data in Zoho CRM

Delete data from Zoho CRM

This example demonstrates how to fetch the account by name and then delete it from the database.

First, retrieve the account with the name "New Company Inc." from the database. Once you have the account, remove it from DbContext and save the changes.

This is how you can implement this in your Program class:

static void Main(string[] args)
{
	RemoveAccountByName("New Company Inc.");
	DisplayAccounts();
}

static void RemoveAccountByName(string accountName)
{
	using (var context = new ZohoModel())
	{
    	// Fetch the account with the specified name
    	var account = context.Accounts
        	.FirstOrDefault(a => a.AccountName == accountName);

    	if (account != null)
    	{
        	// Remove the account from the context
        	context.Accounts.Remove(account);

        	// Save changes to the database
        	context.SaveChanges();

        	Console.WriteLine($"Account with name {accountName} removed successfully.");
    	}
    	else
    	{
        	Console.WriteLine($"Account with name {accountName} not found.");
    	}
	}
}

Here is what we get.

Delete data from Zoho CRM

Conclusion

In this tutorial, we explored how the integration of Entity Framework Core with Zoho CRM using dotConnect for Zoho CRM simplifies data management in .NET applications. By combining the advanced capabilities of dotConnect for Zoho CRM with the power of Entity Framework Core, you can efficiently handle database operations in Zoho-powered applications. With these tools, you can develop scalable, maintainable, and robust .NET solutions that seamlessly interact with Zoho CRM data.

dotConnect for Zoho CRM

Get an enhanced ORM-enabled data provider for Zoho CRM and develop .NET applications working with Zoho CRM data quickly and easily!

Discover the ultimate capabilities of dotConnect for Zoho CRM Download free trial