Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/CourseRegistration.API/CourseRegistration.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.11">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.11" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
Expand Down
3 changes: 3 additions & 0 deletions api/CourseRegistration.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CourseRegistrationDb;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.11">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public CourseRegistrationDbContext(DbContextOptions<CourseRegistrationDbContext>
/// </summary>
public DbSet<Registration> Registrations { get; set; } = null!;

/// <summary>
/// Certificates DbSet
/// </summary>
public DbSet<Certificate> Certificates { get; set; } = null!;

/// <summary>
/// Configures the model relationships and constraints
/// </summary>
Expand Down Expand Up @@ -108,6 +113,41 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(r => r.CourseId)
.OnDelete(DeleteBehavior.Cascade);
});

// Configure Certificate entity
modelBuilder.Entity<Certificate>(entity =>
{
entity.HasKey(c => c.CertificateId);
entity.Property(c => c.CertificateId).IsRequired();
entity.Property(c => c.StudentId).IsRequired();
entity.Property(c => c.CourseId).IsRequired();
entity.Property(c => c.IssueDate).IsRequired();
entity.Property(c => c.FinalGrade).IsRequired()
.HasConversion<string>();
entity.Property(c => c.CertificateNumber).IsRequired().HasMaxLength(20);
entity.Property(c => c.Remarks).HasMaxLength(200);
entity.Property(c => c.DigitalSignature).HasMaxLength(100);

// Create index on CertificateNumber for quick lookups
entity.HasIndex(c => c.CertificateNumber).IsUnique();

// Create index on StudentId for efficient student certificate queries
entity.HasIndex(c => c.StudentId);

// Create index on CourseId for efficient course certificate queries
entity.HasIndex(c => c.CourseId);

// Configure relationships
entity.HasOne(c => c.Student)
.WithMany()
.HasForeignKey(c => c.StudentId)
.OnDelete(DeleteBehavior.Restrict);

entity.HasOne(c => c.Course)
.WithMany()
.HasForeignKey(c => c.CourseId)
.OnDelete(DeleteBehavior.Restrict);
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace CourseRegistration.Infrastructure.Data;

/// <summary>
/// Design-time factory for CourseRegistrationDbContext to support EF Core migrations
/// </summary>
public class CourseRegistrationDbContextFactory : IDesignTimeDbContextFactory<CourseRegistrationDbContext>
{
/// <summary>
/// Creates a new instance of CourseRegistrationDbContext for design-time operations
/// </summary>
public CourseRegistrationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<CourseRegistrationDbContext>();

// Use SQL Server for migrations
// Connection string can be overridden via environment variable
var connectionString = Environment.GetEnvironmentVariable("MIGRATION_CONNECTION_STRING")
?? "Server=localhost,1433;Database=CourseRegistrationDb;User Id=sa;Password=YourStrong!Passw0rd;TrustServerCertificate=True;MultipleActiveResultSets=true";

optionsBuilder.UseSqlServer(
connectionString,
options => options.MigrationsAssembly("CourseRegistration.Infrastructure"));

return new CourseRegistrationDbContext(optionsBuilder.Options);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading