Blazor Helpers App: EF Migrations and CRUD Operations
blazor blazor entity-framework csharp blazor-server code-first
The previous two posts went into detail about getting a Code First database Entity Framework setup for a Blazor Server app in a transparent manner. Let’s now look at adding some CRUD functionality to app’s service.
About Blazor Hosting Options: here
Whilst not wishing to bore the well versed in this area, here is a quick summary of CRUD. CRUD are operations with a relational database application. The relational term means the entities (manifested as a database table) are interrelated. A Person entity many be the designated person property of an Activity. “A person will perform an activity”. In the Activity database table there will be a Person column. This column will only use the Id of the person in the Person table. CRUD means Create, Read, Update and Delete operations with database. That is, add (Create) new tables and records therein, get (read) records, update existing records with changes as well as to delete them. Note that referential integrity requires that if a record in an entity is deleted, say one person, then all records in tables that have a reference to this person need to be deleted or that column for the record needs to be nulled. The Id field of an entity is called the Primary Key (PK) where as a referred entity property is called a Foreign Key (FK). Each PK in a table must be unique as it’s the identity of a record whereas the FK need not. “A Person may perform more than one Activity”.
Code-First means that you don’t manually create the database, its tables and columns and set the interrelations. It means the entities are defined as classes with the interrelations defined by defined entities being properties of other entities. With Entity Framework, having setup the classes you apply a couple of commands, at a high level to seamlessly generate the database and its tables. The table name is defined in the database connection string (in appsettings.json) and does not require pre-creation. add-migration and update-database cause the SQL database to be generated.
Entity Framework creates all of the scaffolding for database CRUD operations when the add-migration is performed for a Code-First application and is pushed out to the database when update-database is actioned. This scaffolding is coded in files in the Data/Migrations folder of C# Blazor Server project. New migrations build like a software stack in that as entity changes are made with the project (adding, deleting and modifying entity classes) and add-migration is applied there are files generated to both apply the changes and to also undo the changes in the target database. Its worth noting that some low level functionality can be embedded in the SQL database, or seamlessly implemented in the EF code; particularly where that functionality is not available in database.
EF Migrations
This is implemented in the Basic branch of djaus2/EFBlazorBasic on GitHub
There are two EF Migrations for the app. The first is for the initial setup of the app. The second is AddEntities Migration after the Helper app entities were added.
Migrations for the Helper App.
Note that the current state of play is in the Snapshot.
An outline of the 20210130014717_AddEntities.cs migration file:
public partial class AddEntities : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable( name: "Helpers",
... More ...
};
migrationBuilder.CreateTable( name: "Rounds",
... More ...
};
migrationBuilder.CreateTable( name: "Activitys",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
HelperId = table.Column<int>(type: "int", nullable: true)
...etc ...
},
constraints: table =>
{ table.PrimaryKey("PK_Activitys", x => x.Id);
table.ForeignKey( helper: "FK_Activitys_Helpers_HelperId", ...
... etc ...
};
});
migrationBuilder.CreateIndex( name: "IX_Activitys_HelperId",
table: "Activitys", column: "HelperId");
... etc ...
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable( name: "Activitys");
... etc ...
}
}
- View the complete 20210130014717_AddEntities.cs file on GitHub
- View all Migration files for the Helper app on GitHub
Blazor Data Service
In a Blazor app, the CRUD operations are embellished as methods in a Service class for a Blazor service only app. For a Blazor WASM app, where data needs to be passed or from the client over REST, a Controller class in the server is used to implement Get,Set,Update and Delete over http. There is though a one-to-one correspondence between the Controller functions and the Service methods. The coding is quite similar.
EF Versions
Blazor (.NET 5) uses Entity Framework Core, “an open-source, lightweight, extensible and a cross-platform version”, building upon .NET Core simplicity. Whilst the default in a new Blazor app is a SQL .mdf file, EF Core supports the following database providers: SQL Server (File, Express, Server and Azure SQL), MySQL, PostgreSQL, SQLite, SQL Compact and In-memory.
Next: CRUD methods in a Blazor Service.
Topic | Subtopic | |
This Category Links | ||
Category: | Blazor Index: | Blazor |
Next: > | Blazor Helpers App | Add Some Data |
< Prev: | Blazor Helpers App | Entity Framework Summary |