Blazor Helpers App: Entity Framework Basics
blazor blazor entity-framework csharp blazor-server
Following on from the previous post, this post discusses Code First data access to a SQL Server Database in a Blazor Service app using Entity Framework Core.
The app is a Blazor Web Service for helpers to volunteer for assistance tasks at a track meet with SMS based Y/N confirmations. Entity Framework Core is used with the main entities being Round, Helper and Activity. A Helper volunteers and performs an Activity at a Meet (called a Round).
Being a Blazor Service app, not involving WASM, all data processing, form handling and rendering thereof is handled by a service on the server. There is no use of Get, Put, Update and Delete over HTTP. The database CRUD is handled directly by a service using Code First Entity Framework Core via the ApplicationDbContext. Each of the data entities are attached to that. The ApplicationDbContext code is autogenerated when the project is created by choosing the Identity option.
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions\<ApplicationDbContext\> options) : base(options) {}
public DbSet<Activity> Activitys { get; set; }
public DbSet<Helper> Helpers { get; set; }
public DbSet<Round> Rounds { get; set; }
}
The ApplicationDbContext including Entity Contexts
Each of the entities is defined in a class and .. Migrations are used to generate the database tables. An activity has one Round and one Helper and so it has those as Foreign Key properties. There are other entities for such things as logging which will be discussed in later posts. Suffice to say, that there is cross referencing in them as well. ..Migrations is a great way to build and update the database tables as a change to code in an entity class (add, remove or change a property) is automatically pushed through to its database table.
Each entity is a table in the database. Each record in a table has a Primary Key (a unique integer or GUID). If in one entity you declare another entity is a property in it, then that becomes a Foreign key. The first database table only stores the primary key of the secondary entity. This is all handled seamlessly by Entity Framework.
public class Activity
{
[Key]
[Column("Id")]
public int Id { get; set; }
[Column("Round")]
[Required]
public Round Round { get; set; }
[Column("Helper")]
public Helper Helper { get; set; }
[Column("Task")]
[Required]
public string Task { get; set; }
}
The Activty Class
Round and Helper are referenced by the Activity. Note, the Helper is not annotated as [Required] and is initially null. When a helper volunteers, they are assigned to this property. Each Round has a date an track location. The Helper has name, email and mobile properties. The Activity has other properties such as start time, duration, pit number etc.
A service runs as part of the Blazor Service for exercising the database CRUD operations. This service notes the ApplicationDBContext at instantiation which is used to access the entity’s tables in the database.
namespace AthsEssAVHelpers.Data
{
public interface IDataAccessService
{
Task<List<Activity>> GetActivitys();
Task AddActivity(Activity activity);
Task RemoveActivity(int Id);
Task UpdateActivity(int activityId, int helperId, int assigneeId);
...
...
}
public class DataAccessService : IDataAccessService
{
private readonly ApplicationDbContext _context;
public DataAccessService(ApplicationDbContext context)
{
_context = context;
}
public async Task<List<Activity>> GetActivitys()
{
var list = await _context.Activitys.Include(activity => activity.Helper).Include(activity => activity.Round).ToListAsync();
return list;
}
public async Task AddActivity(Activity activity)
{
_context.Activitys.Add(activity);
await _context.SaveChangesAsync();
}
...
...
}
}
The DataAccessService
Next: Database creation using Entity Framework Core.
Topic | Subtopic | |
This Category Links | ||
Category: | Blazor Index: | Blazor |
Next: > | Blazor Helpers App | An Entity Framework Primer |
< Prev: | Blazor Helpers App | Overview of a Database oriented App |