Creating ASP.Net Web API With EF Database

Introduction

In this article we will see how to create the ASP. NET Web API With an Entity Framework (EF) Database.

By using the EF database, developers can work easily with an object model that can be mapped to various storage schemas, and can be implemented within a different database.

Now the procedure for creating the ASP. NET Web API with an EF Database.

Step 1

First we create the Web API application:

  • Start the Visual Studio 2010
  • Click on "New Project" from the Start Page.
  • The New Project window opens; choose ASP. NET MVC4 Web Application and click on the "OK" button.

    ef.jpg

  • Now open a new ASP. NET MVC4 Project template.
  • We choose Web API and click on the "OK" button.

    ef1.jpg

Step 2

We create the SDF Database in the App_Data folder.

  • In  the Solution Explorer.
  • Right-click on the App_Data folder
  • Select "Add" -> "New Item".

    ef3.jpg

  • Open a window; from that window we select the SQL Server Compact 4.0 Local Database.
  • And change the name to "Detail.sdf".
  • Click on the "OK" button.

Step 3

We create the DataModel in the Model folder using the following procedure:

  • In the Solution Explorer.
  • Right-click on the Model folder.
  • Select "Add" -> "New Item".

    1.jpg

  • Open window then select the ADO.NET Entity model.
  • Change its name to "DetailModel.edmx".
  • Click on OK button.

The Entity Detail Model Wizard is open then:

  • From the wizard we select Empty model.
  • And click on the "Finish" button.

    ef7.jpg

Now open a Tool box. From the Tool box we drag the Entity in to the surface. And name it as DetailModel. We add the field name into this entity.

2.jpg

ef11.jpg

Step 4

We generate the database script using the following procedure:

  • Right-click on surface.
  • Select the Generate Database Model.
  • Open the Generate Database Wizard.

    ef8.jpg

  • The Detail Model Database is selected by default.
  • Press the "Next" button.

    ef9.jpg

  • Press the Finish button.
  • A "Detail.edmx.sqlc" file is generated.

Step 5

Now we execute the database.

First we install the SQL CE Toolbox. We we install it. Then it will be visible in the View/Other Windows/SQL Server Compact Toolbox.

Now we open it.

  • View/Other Windows/SQL Server Compact Toolbox.

    ef10.jpg

  • The SQL Server Compact Toolbox is opened.
  • Right-click on the "DetailModelContainer" -> "Open SQL Editor".

    ef12.jpg

  • When it is open, copy the contents of Detail.edmx.sqlc and paste it on this SQL Editor.

The copied code is this:

  1. -- --------------------------------------------------  
  2. -- Entity Designer DDL Script for SQL Server Compact Edition  
  3. -- --------------------------------------------------  
  4. -- Date Created: 06/10/2013 13:23:21  
  5. -- Generated from EDMX file: D:\mudita\projects\MvcApplication9\MvcApplication9\Models\TodoModel.edmx  
  6. -- --------------------------------------------------  
  7.   
  8. -- --------------------------------------------------  
  9. -- Dropping existing FOREIGN KEY constraints  
  10. -- NOTE: if the constraint does not exist, an ignorable error will be reported.  
  11. -- --------------------------------------------------  
  12.   
  13. -- --------------------------------------------------  
  14. -- Dropping existing tables  
  15. -- NOTE: if the table does not exist, an ignorable error will be reported.  
  16. -- --------------------------------------------------  
  17.   
  18. -- --------------------------------------------------  
  19. -- Creating all tables  
  20. -- --------------------------------------------------  
  21.   
  22. -- Creating table 'TodoModels'  
  23. CREATE TABLE [TodoModels] (  
  24.     [Id] int IDENTITY(1,1) NOT NULL,  
  25.     [Name] nvarchar(4000)  NOT NULL,  
  26.     [Date] datetime  NOT NULL  
  27. );  
  28. GO  
  29.   
  30. -- --------------------------------------------------  
  31. -- Creating all PRIMARY KEY constraints  
  32. -- --------------------------------------------------  
  33.   
  34. -- Creating primary key on [Id] in table 'TodoModels'  
  35. ALTER TABLE [TodoModels]  
  36. ADD CONSTRAINT [PK_TodoModels]  
  37.     PRIMARY KEY ([Id] );  
  38. GO  
  39.   
  40. -- --------------------------------------------------  
  41. -- Creating all FOREIGN KEY constraints  
  42. -- --------------------------------------------------  
  43.   
  44. -- --------------------------------------------------  
  45. -- Script has ended  
  46. -- --------------------------------------------------  

 

Now execute the application.

Step 6

Add the demo data.

  • Double-click on "Detail.sdf".
  • Open the Server Explorer.
  • Right-click on the table then select "Create table".

    ef13.jpg
    Add some data to the table:
  • In the Table folder right-click on the Detail table.
  • And add some data.

    ef15.jpg


Similar Articles