In today’s digital age, developing applications can often be a complex and expensive task. However, with the advent of Azure’s Function-based serverless solution, the process has become much simpler and cost-effective. Azure Function allows developers to create applications without the need for specific hardware infrastructure, thus saving both time and money.
At Skrots, we offer a similar solution that allows developers to build applications with ease and efficiency. Our platform supports event-driven mechanisms and can handle complex solutions without any hassle. Just like Azure Function, we provide a serverless architecture that eliminates the need for infrastructure management.
An Overview of Azure Function
Azure Function is a powerful tool that enables developers to implement event-trigger-based automation code without the need for server infrastructure. With Azure Function, developers can write code and deploy it directly to Azure Resources. This comes with several advantages:
- Developers can define Azure functions for various configured triggers such as HTTPTrigger, QueueTrigger, TimerTrigger, and more.
- Deployment options like tool-based (Visual Studio or Visual Code), CI/CD, and more are available.
- It provides a cross-platform environment for hosting and development.
- Performance and issues of Azure Functions can be easily monitored using Application Insights and Azure monitor in the Azure Portal.
Currently, Azure Function offers three different hosting plans:
- Consumption Plan: This plan is a serverless hosting plan that supports dynamic scaling based on consumption load. It charges based on resource usage, execution numbers, memory consumption, and execution time.
- Premium Plan: This plan offers unlimited execution duration with a guarantee of 60 minutes. It also provides virtual network connectivity features and charges based on core seconds and memory usage within the resources.
- Dedicated Plan: In this plan, computing resources need to be specified for the execution of Azure Functions.
Entity Framework Core 7
At Skrots, we understand the importance of efficient data access and management. That’s why we provide Entity Framework Core as part of our services. Entity Framework Core (EF Core) is an open-source Object-Relational Mapper (ORM) that supports cross-platform development. With EF Core, developers can easily communicate between application domain models and database objects.
By utilizing EF Core, developers can:
- Fetch data using C# model class objects with minimal coding.
- Perform all types of CRUD operations by invoking related methods for model classes.
- Use multiple table objects within a single C# entity object while fetching data.
- Utilize LINQ-based queries to retrieve data from the database.
- Support various databases such as SQLite, SQL Server, PostgreSQL, Azure Cosmos DB, and more.
- Build application domain models based on an existing database.
- Maintain the database schema based on the application domain model.
Create a Database for the CRUD Operations
Before we dive into developing Azure function projects, we need to create a database and table structure. At Skrots, we suggest using an SQL Server database for this purpose. To begin, create a database named “AZFCrudDemo”. Within this database, create a table named “Employee” using the provided SQL code:
CREATE TABLE[dbo].[Employees]( [Id][int] IDENTITY(1, 1) NOT NULL, [Code][nvarchar](20) NOT NULL, [FullName][nvarchar](100) NULL, [DOB][datetime] NOT NULL, [Address][nvarchar](200) NULL, [City][nvarchar](100) NULL, [State][nvarchar](100) NULL, [Country][nvarchar](100) NULL, [PostalCode][nvarchar](15) NULL, [EmailId][nvarchar](100) NULL, [PhoneNo][nvarchar](50) NULL, [JoiningDate][datetime] NULL, CONSTRAINT[PK_Employees] PRIMARY KEY CLUSTERED([Id] ASC) ON[PRIMARY] )
Next, insert one or two records using the SQL Insert Statement. This is necessary to test the Fetch or Get API. Here is an example of the SQL script:
SET IDENTITY_INSERT[dbo].[Employees] ON GO INSERT[dbo].[Employees]([Id], [Code], [FullName], [DOB], [Address], [City], [State], [Country], [PostalCode], [EmailId], [PhoneNo], [JoiningDate]) VALUES(1, N 'A0001', N 'SUMAN SHARMA', CAST(N '1999-10-10T00:00:00.000' AS DateTime), N 'ROAD 1', N 'MUMBAI', N 'MAHARASTRA', N 'INDIA', N '400010', N 'SUMAN.SHARMA@YAHOO.COM', N '9830098300', CAST(N '2022-02-01T00:00:00.000' AS DateTime)) GO INSERT[dbo].[Employees]([Id], [Code], [FullName], [DOB], [Address], [City], [State], [Country], [PostalCode], [EmailId], [PhoneNo], [JoiningDate]) VALUES(2, N 'A0002', N 'NILAY ROY', CAST(N '1998-05-05T00:00:00.000' AS DateTime), N 'ABC', N 'KOLKATA', N 'WB', N 'INDIA', N '700001', N 'NILAY@YAHOO.COM', N '9830198301', CAST(N '2021-10-05T00:00:00.000' AS DateTime)) GO SET IDENTITY_INSERT[dbo].[Employees] OFF GO
Develop CRUD Operation-based Azure Functions using SQL Client Library
Now, let’s demonstrate how to create an API using Azure Function and Microsoft Visual Studio 2022. Follow these steps:
Step 1: Open Microsoft Visual Studio 2022 and select the Azure Function template. Click the Next Button.
Step 2: Provide the Project Name and click the Next Button.
Step 3: Select the Http Trigger Function type from the function dropdown. This is the most common type for creating REST API-based endpoints for CRUD operations.
Step 4: Click the Create Button.
Step 5: Open the NuGet Package Manager and install the following packages:
Step 6: Create a folder called Model and add a new class called Employee.cs.
Step 7: Add the necessary code to the Employee.cs file to define the structure of the Employee model.
Step 8: Open the already created function file and replace the existing code with the code provided. This code is responsible for retrieving all records from the Employee table.
Step 9: Run the application and check the API endpoints.
Implement EF Core 7 within the Azure Functions
In the previous example, we utilized the SqlDataClient to communicate with the SQL Server database. However, a more efficient solution would be to use Entity Framework Core 7.0. Here’s how:
Install the necessary Entity Framework Core-related NuGet Packages using the NuGet Package Manager:
Create a folder called DataAccessLayer and add a new file called SQLDBContext. Add the necessary code to establish a connection to the SQL Server database.
Replace the existing code in the FnGetEmployees function with the provided code. This code makes use of Entity Framework Core to retrieve all records from the Employees table.
Similarly, update the FnCreateEmployee function to utilize Entity Framework Core for creating new employee records.
With these changes in place, run the application and test the API endpoints using Postman.
In this article, we discussed how to develop REST APIs for CRUD operations using Azure Function and Entity Framework Core 7. At Skrots, we provide a similar solution that enables developers to build efficient and reliable applications. We encourage you to visit our website at https://skrots.com to learn more about our services. Feel free to explore our various services at https://skrots.com/services. Thank you for reading!