Blazor

Let's Create an Shoe Inventory Management System using Blazor. - Part 2

We have setup almost complete in Part 1. We just need to configure the database before checking our application.

Hugh Flanagan

To create the database for your inventory management system using Entity Framework Core, follow these steps (From Part 1.):

Step 1: Add Database Configuration to Infrastructure Layer

  1. Install Entity Framework Core Packages:
  • Open NuGet Package Manager in Visual Studio.
  • Install the following packages for the InventoryManagement.Infrastructure project:
  • Microsoft.EntityFrameworkCore.SqlServer – For SQL Server database support.
  • Npgsql.EntityFrameworkCore.PostgreSQL – For Postgres database support.
  • Microsoft.EntityFrameworkCore.Tools – For migration management.
  • Microsoft.EntityFrameworkCore.Design – For migration management.
  1. Configure the DbContext:
  • You should already have the InventoryContext defined in the Infrastructure project, which inherits from DbContext. Ensure your InventoryContext is ready and includes all DbSet properties for your entities.
  • Here’s a sample InventoryContext:
using InventoryManagement.Domain.Entities;
using Microsoft.EntityFrameworkCore;

namespace InventoryManagement.Infrastructure
{
    public class InventoryContext : DbContext
    {
        public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }

        public DbSet<Cleat> Cleats { get; set; }
    }
}
  1. Add Connection String:
  2. Open appsettings.json in the InventoryManagement.Web project.
  3. Add the connection string for SQL Server:
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=YourServerName;Database=InventoryManagementDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
  // Other settings...
}
  1. Connection String for PostgresQL
{
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost; Database=dotnet-6-crud-api; Username=postgres; Password=mysecretpassword"
  }
  // Other settings...
}
  1. 2. Replace YourServerName with your SQL Server instance name. You can also change InventoryManagementDB to your preferred database name.
  2. Register DbContext in Program.cs:
  3. In the InventoryManagement.Web project, update Program.cs to register InventoryContext with the DI container:



using InventoryManagement.Infrastructure;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

// Register DbContext
builder.Services.AddDbContext<InventoryContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();

Step 2: Create Initial Migration

  • Open Package Manager Console:
  • Go to Tools > NuGet Package Manager > Package Manager Console in Visual Studio.
  • Set the InventoryManagement.Infrastructure project as the Default Project.
  • Run Migration Command:
  • Create the initial migration by running:
Add-Migration InitialCreate
  • This command will create a migration file in the Infrastructure project under a Migrations folder, containing the code required to create the database schema.

Step 3: Apply the Migration to Create the Database

  • Run Database Update Command:
  • In the Package Manager Console, apply the migration by running:
Update-Database
  • This command will create the database (InventoryManagementDB) and the required tables (e.g., Cleats) based on your InventoryContext.
  • Verify the Database:
  • Open SQL Server Management Studio (SSMS) or your preferred SQL management tool.
  • Connect to your SQL Server instance and verify that the InventoryManagementDB database and the associated tables were created successfully.

Step 4: Now we can run our Application.

  • Run the Application:
  • Press F5 to run the solution. (Before we run, we need to setup database. Let's do that in Part 2)
  • Navigate to /cleats to see the inventory.


Summary of Steps:

  1. Configure DbContext and Add Connection String:
  2. Ensure InventoryContext is properly configured in the Infrastructure project.
  3. Add a connection string in appsettings.json in the Web project.
  4. Register DbContext:
  5. Register InventoryContext in the DI container in Program.cs.
  6. Create Migration:
  7. Use Package Manager Console to add a migration (Add-Migration InitialCreate).
  8. Update Database:
  9. Apply the migration using Update-Database.
  10. Run Application:
  11. F5

These steps will ensure your database is created based on the entity definitions and can be used within your application. If you need to make changes to your database schema, you can modify your entity models and run further migrations to update the database schema accordingly.

Become a member
Get the latest news right in your inbox. It's free and you can unsubscribe at any time. We hate spam as much as we do, so we never spam!
Read next

Let's Fix Blog Code to be cross platform compatible.

In using the Blazing Blog V2 template for our blog, I found that it was written to work on Windows. It is not difficult to make your code cross-platform compatible. Check out the following examples.

Hugh Flanagan
An unhandled error has occurred. Reload 🗙