Introduction
This article will give you step-by-step instructions for creating an ASP.NET Web API service, using Entity Framework, and publishing it in Azure.
Prerequisites
Basic knowledge of ASP.NET Web API, Entity Framework, Kendo UI Framework, and Azure Web apps.
Create an SQL Database in Azure
Let's start with creating a database in Azure. Please check here how to create a new database in Azure.
I have created a new database in Azure. Connect the database in SSMS (SQL Studio Management Service), as shown in the image below.

The script for creating a table is given below.
CREATE TABLE Employee
(
EmployeeID INT IDENTITY(1,1) CONSTRAINT Pk_Emp_Id PRIMARY KEY,
FirstName VARCHAR(20),
LastName VARCHAR(20)
);
Insert some sample records, as shown below.
INSERT INTO Employee VALUES('Bob','Ross');
INSERT INTO Employee VALUES('Pradeep','Raj');
INSERT INTO Employee VALUES('Arun','Kumar');
INSERT INTO Employee VALUES('Vasanth','Kumar');
Create a new ASP.NET Web API application
Create a new ASP.NET Web API Application, as per the following figures. Open Visual Studio ->File ->New project ->ASP.NET Web Application.


Make sure, the host in Cloud is checked and click OK.

Once Azure Web app settings are configured, click OK.
Let us start creating a Model in the application.
Generate the Model
Now, we will create Entity Framework Models from the database tables.
Step 1. Right-click the Models folder, select Add, and New Item.
Step 2. In the Add New Items window, select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new Model file as Employee and click Add.
Step 3. In the Entity Data Model wizard, select EF Designer from the database and click Next.

Step 4. Click the New Connection button.
Step 5. In the Connection Properties window, provide the name of the Azure SQL Server where the database was created. After providing the Server name, select Employee from the available databases and click OK.

Step 6. You can use the default name for connection to save on the Web. Config the file and click Next.
Step 7. Select the table to generate models for the Employee table and click Finish.
Create a Controller
Create a new empty Controller. Right-click the Controllers folder and select Add –> New Empty Web API 2 Controller. In my case, I named it as EmployeeCRUD Controller.
Write the code, given below, in the Controller file.
public class EmployeesController : ApiController
{
private EmployeeEntities db = new EmployeeEntities();
// GET: api/Employees
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
// PUT: api/Employees/5
public HttpResponseMessage PutEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
db.Entry(employee).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
// POST: api/Employees
public HttpResponseMessage PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.EmployeeID }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
// DELETE: api/Employees/5
public HttpResponseMessage DeleteEmployee(Employee employee)
{
Employee remove_employee = db.Employees.Find(employee.EmployeeID);
if (remove_employee == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Employees.Remove(remove_employee);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(int id)
{
return db.Employees.Count(e => e.EmployeeID == id) > 0;
}
}









DhanuOPPosted Feb 7, 2024, 9:39 AM
Without entity framework
Rahul PatilPosted Aug 7, 2020, 7:12 AM
NOT ALL INFORMATION WHERE IS CONNECTION WINDOW IMAGE PLEASE PROVIDE ALL INFORMATION
Raul PontesPosted Jan 18, 2019, 12:39 AM
I get "No HTTP resource was found that matches the request URI 'https://apitestergdp.azurewebsites.net/api/employee'." .. cound't make it work... anyone can help me?
Tugrul GucluPosted Oct 3, 2018, 5:42 PM
Hi I can't find the EmployeeEntities too. If you perform a page search you will see it's been used without declaration. Or am I missing sth ?
Joel MoraisPosted Jul 31, 2018, 6:00 AM
"EmployeeEntities could not be found" Am I missing something here ? I've been stuck here for a long time...
Casie SchullerPosted Apr 13, 2018, 11:16 AM
Thank you for sharing this. It helped me build my first api against an existing azure sql DB! One note, I did find a bug in the code. The if (!ModelState.IsValid) for PostEmployee should be if (ModelState.IsValid), otherwise, the controller returns an error when it is actually posting successfully.
Casie SchullerPosted Apr 11, 2018, 3:30 PM
People who are able to get this working must be doing something that is not included in the instructions. I've followed them verbatim, and when I try to access api/Employee, I get: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
ElPincheZuryPosted Nov 23, 2017, 2:31 PM
I have a problem when i create a new record, the ID is always 0, and i dont know what to do
Basudeb BanerjeePosted Jul 6, 2017, 5:01 AM
With azure free trial service can I create database in azure by the above mentioned steps free of cost.
sreenivasa kPosted Sep 6, 2016, 7:43 AM
Nice one.
Tarun DPosted Sep 6, 2016, 7:16 AM
Nice article. Thanks for sharing..