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:

Step 2

We create the SDF Database in the App_Data folder.

Step 3

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

The Entity Detail Model Wizard is open then:

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:

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.

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. -- Dropping existing FOREIGN KEY constraints
  9. -- NOTE: if the constraint does not exist, an ignorable error will be reported.
  10. -- --------------------------------------------------
  11. -- --------------------------------------------------
  12. -- Dropping existing tables
  13. -- NOTE: if the table does not exist, an ignorable error will be reported.
  14. -- --------------------------------------------------
  15. -- --------------------------------------------------
  16. -- Creating all tables
  17. -- --------------------------------------------------
  18. -- Creating table 'TodoModels'
  19. CREATE TABLE [TodoModels] (
  20. [Id] int IDENTITY(1,1) NOT NULL,
  21. [Name] nvarchar(4000) NOT NULL,
  22. [Date] datetime NOT NULL
  23. );
  24. GO
  25. -- --------------------------------------------------
  26. -- Creating all PRIMARY KEY constraints
  27. -- --------------------------------------------------
  28. -- Creating primary key on [Id] in table 'TodoModels'
  29. ALTER TABLE [TodoModels]
  30. ADD CONSTRAINT [PK_TodoModels]
  31. PRIMARY KEY ([Id] );
  32. GO
  33. -- --------------------------------------------------
  34. -- Creating all FOREIGN KEY constraints
  35. -- --------------------------------------------------
  36. -- --------------------------------------------------
  37. -- Script has ended
  38. -- --------------------------------------------------

Now execute the application.

Step 6

Add the demo data.