In SQL, a view is a virtual table based on the result-set of an SQL statement. They are typically used as read-only objects that are optimized to provide data for a given scenario. Entity Framework Core 5 can handle views and in this article, I’m going to show you how.
First of all, we need to add a view to the database. The best way to do so is to add a database migration with an appropriate SQL. Let’s start by adding a migration with EF Core global tool command:
1
dotnet ef migrations add vwGuestArrivals
This will generate a migration, that we can put our SQL into. Let’s see how it may look:
public partial class vwGuestArrivals : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
var sql = @"
CREATE OR ALTER VIEW [dbo].[vwRoomsOccupied] AS
SELECT r.[From], r.[To], ro.Number As RoomNumber, ro.Level, ro.WithBathroom
FROM Reservations r
JOIN Rooms ro ON r.RoomId = ro.Id";
migrationBuilder.Sql(sql);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"DROP VIEW vwRoomsOccupied");
}
}
This view presents rooms that are occupied, where we can filter by date. This kind of data can be useful for example when planning maintenance.
#asp.net core for .net 5 & ef core 5 #ef core 5 #primehotel #view