"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

Add Models and Controllers

by Mike Wasson

Download Completed Project

In this section, you will add model classes that define the database entities. Then you will add Web API controllers that perform CRUD operations on those entities.

Add Model Classes

In this tutorial, we’ll create the database by using the “Code First” approach to Entity Framework (EF). With Code First, you write C# classes that correspond to database tables, and EF creates the database. (For more information, see Entity Framework Development Approaches.)

We start by defining our domain objects as POCOs (plain-old CLR objects). We will create the following POCOs:

In Solution Explorer, right click the Models folder. Select Add, then select Class. Name the class Author.

Replace all of the boilerplate code in Author.cs with the following code.

[!code-csharpMain]

   1:  using System.Collections.Generic;
   2:  using System.ComponentModel.DataAnnotations;
   3:   
   4:  namespace BookService.Models
   5:  {
   6:      public class Author
   7:      {
   8:          public int Id { get; set; }
   9:          [Required]
  10:          public string Name { get; set; }
  11:      }
  12:  }

Add another class named Book, with the following code.

[!code-csharpMain]

   1:  using System.ComponentModel.DataAnnotations;
   2:   
   3:  namespace BookService.Models
   4:  {
   5:      public class Book
   6:      {
   7:          public int Id { get; set; }
   8:          [Required]
   9:          public string Title { get; set; }
  10:          public int Year { get; set; }
  11:          public decimal Price { get; set; }
  12:          public string Genre { get; set; }
  13:   
  14:          // Foreign Key
  15:          public int AuthorId { get; set; }
  16:          // Navigation property
  17:          public Author Author { get; set; }
  18:      }
  19:  }

Entity Framework will use these models to create database tables. For each model, the Id property will become the primary key column of the database table.

In the Book class, the AuthorId defines a foreign key into the Author table. (For simplicity, I’m assuming that each book has a single author.) The book class also contains a navigation property to the related Author. You can use the navigation property to access the related Author in code. I say more about navigation properties in part 4, Handling Entity Relations.

Add Web API Controllers

In this section, we’ll add Web API controllers that support CRUD operations (create, read, update, and delete). The controllers will use Entity Framework to communicate with the database layer.

First, you can delete the file Controllers/ValuesController.cs. This file contains an example Web API controller, but you don’t need it for this tutorial.

Next, build the project. The Web API scaffolding uses reflection to find the model classes, so it needs the compiled assembly.

In Solution Explorer, right-click the Controllers folder. Select Add, then select Controller.

In the Add Scaffold dialog, select “Web API 2 Controller with actions, using Entity Framework”. Click Add.

In the Add Controller dialog, do the following:

  1. In the Model class dropdown, select the Author class. (If you don’t see it listed in the dropdown, make sure that you built the project.)
  2. Check “Use async controller actions”.
  3. Leave the controller name as “AuthorsController”.
  4. Click plus (+) button next to Data Context Class.

In the New Data Context dialog, leave the default name and click Add.

Click Add to complete the Add Controller dialog. The dialog adds two classes to your project:

At this point, build the project again. Now go through the same steps to add an API controller for Book entities. This time, select Book for the model class, and select the existing BookServiceContext class for the data context class. (Don’t create a new data context.) Click Add to add the controller.

Previous Next





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-2.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>