diff --git a/src/Services/Customer/Customer.API/Program.cs b/src/Services/Customer/Customer.API/Program.cs index e46940a..2cee388 100644 --- a/src/Services/Customer/Customer.API/Program.cs +++ b/src/Services/Customer/Customer.API/Program.cs @@ -13,6 +13,12 @@ var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.EnsureCreated(); +} + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/src/Services/Customer/Customer.Infrastructure/Data/CustomerDbContext.cs b/src/Services/Customer/Customer.Infrastructure/Data/CustomerDbContext.cs index c986f1e..a8e9e50 100644 --- a/src/Services/Customer/Customer.Infrastructure/Data/CustomerDbContext.cs +++ b/src/Services/Customer/Customer.Infrastructure/Data/CustomerDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Entities = Customer.Domain.Entities; namespace Customer.Infrastructure.Data; @@ -8,9 +9,23 @@ public CustomerDbContext(DbContextOptions options) : base(opt { } + public DbSet Customers => Set(); + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - // TODO: Configure entity mappings migrated from monolith + + // NOTE: Use `Entities.Customer` to disambiguate from the root `Customer` namespace + modelBuilder.Entity(entity => + { + entity.ToTable("AppCustomers"); + entity.Property(c => c.Name).IsRequired().HasMaxLength(100); + entity.HasIndex(c => c.Name); + entity.Property(c => c.Email).HasMaxLength(100); + entity.Property(c => c.PhoneNumber).IsUnicode(false).HasMaxLength(30); + entity.Property(c => c.City).HasMaxLength(50); + entity.Property(c => c.CreatedBy).HasMaxLength(40); + entity.Property(c => c.UpdatedBy).HasMaxLength(40); + }); } } diff --git a/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.Designer.cs b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.Designer.cs new file mode 100644 index 0000000..c8b66ce --- /dev/null +++ b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.Designer.cs @@ -0,0 +1,84 @@ +// +using System; +using Customer.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Customer.Infrastructure.Data.Migrations +{ + [DbContext(typeof(CustomerDbContext))] + [Migration("20260626044302_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Customer.Domain.Entities.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .HasColumnType("text"); + + b.Property("City") + .HasMaxLength(50) + .HasColumnType("character varying(50)"); + + b.Property("CreatedBy") + .HasMaxLength(40) + .HasColumnType("character varying(40)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Gender") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("PhoneNumber") + .HasMaxLength(30) + .IsUnicode(false) + .HasColumnType("character varying(30)"); + + b.Property("UpdatedBy") + .HasMaxLength(40) + .HasColumnType("character varying(40)"); + + b.Property("UpdatedDate") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("AppCustomers", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.cs b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.cs new file mode 100644 index 0000000..2f0b7fd --- /dev/null +++ b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.cs @@ -0,0 +1,50 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Customer.Infrastructure.Data.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AppCustomers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), + Email = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), + PhoneNumber = table.Column(type: "character varying(30)", unicode: false, maxLength: 30, nullable: true), + Address = table.Column(type: "text", nullable: true), + City = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), + Gender = table.Column(type: "integer", nullable: false), + CreatedBy = table.Column(type: "character varying(40)", maxLength: 40, nullable: true), + UpdatedBy = table.Column(type: "character varying(40)", maxLength: 40, nullable: true), + CreatedDate = table.Column(type: "timestamp with time zone", nullable: false), + UpdatedDate = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AppCustomers", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_AppCustomers_Name", + table: "AppCustomers", + column: "Name"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AppCustomers"); + } + } +} diff --git a/src/Services/Customer/Customer.Infrastructure/Data/Migrations/CustomerDbContextModelSnapshot.cs b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/CustomerDbContextModelSnapshot.cs new file mode 100644 index 0000000..bfe54b6 --- /dev/null +++ b/src/Services/Customer/Customer.Infrastructure/Data/Migrations/CustomerDbContextModelSnapshot.cs @@ -0,0 +1,81 @@ +// +using System; +using Customer.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Customer.Infrastructure.Data.Migrations +{ + [DbContext(typeof(CustomerDbContext))] + partial class CustomerDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Customer.Domain.Entities.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .HasColumnType("text"); + + b.Property("City") + .HasMaxLength(50) + .HasColumnType("character varying(50)"); + + b.Property("CreatedBy") + .HasMaxLength(40) + .HasColumnType("character varying(40)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Gender") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("PhoneNumber") + .HasMaxLength(30) + .IsUnicode(false) + .HasColumnType("character varying(30)"); + + b.Property("UpdatedBy") + .HasMaxLength(40) + .HasColumnType("character varying(40)"); + + b.Property("UpdatedDate") + .HasColumnType("timestamp with time zone"); + + b.HasKey("Id"); + + b.HasIndex("Name"); + + b.ToTable("AppCustomers", (string)null); + }); +#pragma warning restore 612, 618 + } + } +}