Skip to content

RazorPageApp

Jon P Smith edited this page Apr 9, 2018 · 11 revisions

The GitHub repo contains an example ASP.NET Core Razor Pages application, in the project RazorPageApp.

You can look at the code, or run the web application locally by cloning the repo and pressing F5.
This web application uses a SQLite in-memory database so that it will run anywhere.

NOTE: See the EF Core DataLayer Wiki page for more about the database and the style of the entity classes in that database.

The GenericServices' command examples

RazorPageApp gives you examples of most of the GenericServices' commands using both direct access and via DTOs. Here is a list of the CRUD pages, and where you can find the code.

1. Create Author and create Book

Here are two quite different ways to create a new entry in the database. A new Author entity is created directly by you creating the entity class directly and asking GenericServices to add it to the database, while a new Book entity, which is much more complex to set up, is created using a static method. Here is the links to the code

  1. Create Author - Authors/Create.cshtml.cs using the entity class.
  2. Create Book - Home/Create.cshtml.cs - this uses the DTO called CreateBookDto.cs

2. ReadMany Author and Book

This shows how to use the ReadManyNoTracked<T> command, which builds an EF Core Select query to show data to the user. Inside GenericServices the Select queries are build using AutoMapper.

  1. ReadMany Author - Authors/Index.cshtml.cs. This uses the simple DTO called AuthorWithBookCountDto.
  2. ReadMany Book - Home/Index.cshtml.cs - this uses the DTO called BookListDto.cs. Because the query is so complex you need to provide extra information to AutoMapper, which you can do via a PerDtoConfig file, in this case called BookListDtoConfig.

3. Update Author and update Book (many)

While the Author update is pretty simple the Book entity has a number of update commands. I suggest you look at some of them to see what they do.

  1. Update Author - See Authors/Edit.
  2. Update Book

*Note: The last one, RemovePromotion, needs to define which method to call because there are no properties to provide, so it can't work out that the RemovePromotion method needs to be called.

4. Delete Author and Book

Deleting seems simple, but it gets complex when you think about the relationships. The two examples, delete an author and delete a book, both have potential relationships that can change things. See the code for more explanations.

  1. Delete Author - see Authors/Delete.cshtml.cs. This stops the delete if the author has any books.
  2. Delete Book - see Home/Delete.cshtml.cs. This would throw an exception if you tried to delete a book that was in an customer's order.

5. Accessing the EF Core via the Context property

Many times you will need to write direct EF Core code to setup other parts of the display or the entity being updated. You can access the instance of the DbContext vai GenericServices' Context property. You can see this in Home/Create.cshtml.cs code.