Hello Friends,
Now I am going to describe this article how to create Web Api Application with Sql Db connection using entity framework.Before go to this concept ,we should know basic approach about entity framework and which approach has used in this application,Next Session i'll describe briefly about entity framework,Please go through this article .I hope it will be very enjoy full for all of my friends.
Step-11.Select first option and click on Next button
Step-12.Select the specific table ,provide name of Model Namespace and click on OK button
Step-13:If this issue will come then simple click on OK button 2-3 time
Step-14:After creation of entity model the screen look like this
Step-15:Create a controller like this
Step-16:Select this Option and click on Add button
Step-17:Provide the Controller name and click on Add button
Step-18:Write below Code in Controller
Page : DoctorsController.cs
Step-19:Go to RouteConfig.cs from App_Data folder file to change default controller to your controller name like this
Page : RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Doctors", action = "Index", id = UrlParameter.Optional }
);
}
Step-20:Build this solution
Step-21:Press F5 to Run this Solution and see below out put.
In this article I’ve explained how to create Web Api Application with Sql Db connection using entity framework in .net application. Please read and Provide your Comments.
Thanks & Regard
Rabi
Now I am going to describe this article how to create Web Api Application with Sql Db connection using entity framework.Before go to this concept ,we should know basic approach about entity framework and which approach has used in this application,Next Session i'll describe briefly about entity framework,Please go through this article .I hope it will be very enjoy full for all of my friends.
Entity Framework : It is a open source ORM(Object Relational Mapping) framework for dot net application by Microsoft.This framework is very user friendly and helpful for dot net developer,Basically It has three approaches
1.Model First:
In Model First approach after creation of entity model then the database will be create by the option of "Generate database from Model" in entity model design screen.
2.Code First:
In Code First Approach first we will create the class then the database will create based on that class.
3.Schema First or Database First:
In this approach we will create the entity framework means the ".edmx" file and class file etc based on the existing database.
In this application i am using "Schema First or Database First" approach
Please the follow below steps to create web api application:
Step-1:Open the Visual Studio and create new project:
Step-2:Select Asp.net web application,provide the name and select the specific location and click on ok button
Step-3:Select Web Apt and click on OK button
Step-4:Create the below table
Step-5:Rightclick your solution ->Add->New Item for create ado.net entity data model
Step-6:Select Data from left side within Visual C# and choose Ado.net Entity Data Model and click on OK button
Step-7:Select highlighted option and click on Next Button
Step-8:Click on Test Connection button then click on OK button to confirm connection is success or not
Step-9:Click on OK button
Step-10.Provide the name and Click on Next button
In this application i am using "Schema First or Database First" approach
Please the follow below steps to create web api application:
Step-1:Open the Visual Studio and create new project:
Step-2:Select Asp.net web application,provide the name and select the specific location and click on ok button
Step-3:Select Web Apt and click on OK button
Step-4:Create the below table
CREATE
TABLE [dbo].[Doctors](
[DoctId] [int] IDENTITY(1,1) NOT NULL,
[Doc_Name] [varchar](50) NULL,
[Qualification] [varchar](50) NULL,
[Address] [varchar](50) NULL
)
ON [PRIMARY]
Insert the records like this:
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Kanaka','BDS','BDK')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Krushna','MBBS','BBSR')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Jyotshna','BDS','BBSR')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Jyotshna','BDS','BBSR')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('J Sendha','MD(Med)','CTC')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Abinash','MS','Jajpur')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Sidhu','MBBS','PURI')
INSERT INTO Doctors(Doc_Name,Qualification,Address)VALUES('Sankar','BDS','BRM')
Step-5:Rightclick your solution ->Add->New Item for create ado.net entity data model
Step-6:Select Data from left side within Visual C# and choose Ado.net Entity Data Model and click on OK button
Step-7:Select highlighted option and click on Next Button
Step-8:Click on Test Connection button then click on OK button to confirm connection is success or not
Step-9:Click on OK button
Step-10.Provide the name and Click on Next button
Step-11.Select first option and click on Next button
Step-12.Select the specific table ,provide name of Model Namespace and click on OK button
Step-13:If this issue will come then simple click on OK button 2-3 time
Step-14:After creation of entity model the screen look like this
Step-15:Create a controller like this
Step-16:Select this Option and click on Add button
Step-17:Provide the Controller name and click on Add button
Step-18:Write below Code in Controller
Page : DoctorsController.cs
using System.Collections.Generic;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web.Http;
namespace WebApiApps.Controllers
{
public class DoctorsController : ApiController
{
private DoctorsEntities doctorsEntities = new DoctorsEntities();
public async
Task<List<Doctor>> GetDoctors()
{
var doctordetail = doctorsEntities.Doctors.ToListAsync();
return await
doctordetail;
}
}
}
Step-19:Go to RouteConfig.cs from App_Data folder file to change default controller to your controller name like this
Page : RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Doctors", action = "Index", id = UrlParameter.Optional }
);
}
Step-20:Build this solution
Step-21:Press F5 to Run this Solution and see below out put.
Thanks & Regard
Rabi
No comments:
Post a Comment