// ============================================================ // PGVECTOR ENTITY + CONFIGURATION TEMPLATE // Generated by MORPH Framework // ============================================================ using System.ComponentModel.DataAnnotations.Schema; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Pgvector; #nullable disable namespace {{NAMESPACE}}.Domain.Entities; /// /// Entity with vector embedding for similarity search via pgvector. /// Dimensions: 1536 (text-embedding-3-small). Adjust if using a different model. /// public class {{pascalCase FEATURE_NAME}} { public Guid Id { get; set; } public string Title { get; set; } = string.Empty; public string Content { get; set; } = string.Empty; public string? Metadata { get; set; } [Column(TypeName = "vector(1536)")] public Vector? Embedding { get; set; } public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow; public DateTimeOffset? UpdatedAt { get; set; } } // ============================================================ // ENTITY TYPE CONFIGURATION // ============================================================ namespace {{NAMESPACE}}.Infrastructure.Data.Configurations; public class {{pascalCase FEATURE_NAME}}Configuration : IEntityTypeConfiguration<{{pascalCase FEATURE_NAME}}> { public void Configure(EntityTypeBuilder<{{pascalCase FEATURE_NAME}}> builder) { builder.ToTable("{{snakeCase FEATURE_NAME}}s"); builder.HasKey(e => e.Id); builder.Property(e => e.Id).HasDefaultValueSql("gen_random_uuid()"); builder.Property(e => e.Title).IsRequired().HasMaxLength(200); builder.Property(e => e.Content).IsRequired(); builder.Property(e => e.Metadata).HasColumnType("jsonb"); builder.Property(e => e.Embedding).HasColumnType("vector(1536)"); builder.Property(e => e.CreatedAt).HasDefaultValueSql("now()"); // HNSW index for cosine similarity search builder.HasIndex(e => e.Embedding) .HasMethod("hnsw") .HasOperators("vector_cosine_ops") .HasStorageParameter("m", 16) .HasStorageParameter("ef_construction", 64); builder.HasIndex(e => e.Title); } } // ============================================================ // SETUP INSTRUCTIONS // ============================================================ // // 1. Install NuGet packages: // // dotnet add package Pgvector.EntityFrameworkCore // dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL // // 2. Configure NpgsqlDataSource with vector support in Program.cs: // // builder.Services.AddNpgsql( // connectionString, // npgsqlOptions => npgsqlOptions.UseVector()); // // 3. Register in DbContext: // // public DbSet<{{pascalCase FEATURE_NAME}}> {{pascalCase FEATURE_NAME}}s => Set<{{pascalCase FEATURE_NAME}}>(); // // protected override void OnModelCreating(ModelBuilder modelBuilder) // { // modelBuilder.HasPostgresExtension("vector"); // modelBuilder.ApplyConfiguration(new {{pascalCase FEATURE_NAME}}Configuration()); // } // // 4. Create and apply migration: // // dotnet ef migrations add Add{{pascalCase FEATURE_NAME}}WithVector -p src/{{NAMESPACE}}.Infrastructure -s src/{{NAMESPACE}}.Web // dotnet ef database update -p src/{{NAMESPACE}}.Infrastructure -s src/{{NAMESPACE}}.Web // // ============================================================