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.

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.

To activate a 30-day trial, use the license key assigned to you after completing the registration on the Devart website.

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.

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.

Follow the detailed illustrated guide to create your database model using Entity Developer. When this process is complete, it opens the model you created, ready for use.

Finalize the model

Entity Developer generates classes for the selected tables, representing data entities. It also creates a ZohoModel class descendant to manage the connection to Zoho CRM and handle 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!

Try the 30-day trial of the full product. No limits. No card required Start free trial