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
- Install Entity Framework Core Packages:
- Open NuGet Package Manager in Visual Studio.
- Install the following packages for the
InventoryManagement.Infrastructureproject: 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.
- Configure the DbContext:
- You should already have the
InventoryContextdefined in the Infrastructure project, which inherits fromDbContext. Ensure yourInventoryContextis ready and includes allDbSetproperties 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; }
}
}
- Add Connection String:
- Open
appsettings.jsonin theInventoryManagement.Webproject. - Add the connection string for SQL Server:
{
"ConnectionStrings": {
"DefaultConnection": "Server=YourServerName;Database=InventoryManagementDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
// Other settings...
}
- Connection String for PostgresQL
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost; Database=dotnet-6-crud-api; Username=postgres; Password=mysecretpassword"
}
// Other settings...
}
- 2. Replace
YourServerNamewith your SQL Server instance name. You can also changeInventoryManagementDBto your preferred database name. - Register DbContext in
Program.cs: - In the
InventoryManagement.Webproject, updateProgram.csto registerInventoryContextwith 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.Infrastructureproject 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 yourInventoryContext. - 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
InventoryManagementDBdatabase 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
/cleatsto see the inventory.
Summary of Steps:
- Configure DbContext and Add Connection String:
- Ensure
InventoryContextis properly configured in the Infrastructure project. - Add a connection string in
appsettings.jsonin the Web project. - Register DbContext:
- Register
InventoryContextin the DI container inProgram.cs. - Create Migration:
- Use Package Manager Console to add a migration (
Add-Migration InitialCreate). - Update Database:
- Apply the migration using
Update-Database. - Run Application:
- 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.
