From ce40abe20faa946dbdb192f876880d8b222f1ade Mon Sep 17 00:00:00 2001 From: OlgunR Date: Mon, 12 Jan 2026 17:00:37 +0100 Subject: [PATCH] Add VwmyCatalog view entity and update repository usage Introduce VwmyCatalog as a keyless entity mapped to the VWMY_CATALOG database view. Update ApplicationDbContext to include the new DbSet and configure its mapping. Modify CatalogRepository to query the view instead of the table for GetAllAsync and GetByIdAsync. Add AutoMapper configuration for VwmyCatalog to Catalog. Add the VwmyCatalog entity class. --- .../ApplicationDbContext.cs | 32 +++++++++++++++++++ .../Mappings/CatalogInfrastructureProfile.cs | 1 + .../Repositories/CatalogRepository.cs | 8 ++--- .../ScaffoldEntities/VwmyCatalog.cs | 18 +++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs diff --git a/DbFirst.Infrastructure/ApplicationDbContext.cs b/DbFirst.Infrastructure/ApplicationDbContext.cs index 6c15cc8..3f7ecea 100644 --- a/DbFirst.Infrastructure/ApplicationDbContext.cs +++ b/DbFirst.Infrastructure/ApplicationDbContext.cs @@ -11,6 +11,7 @@ public partial class ApplicationDbContext : DbContext } public virtual DbSet TbmyCatalogs { get; set; } + public virtual DbSet VwmyCatalogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -49,6 +50,37 @@ public partial class ApplicationDbContext : DbContext .HasColumnName("CHANGED_WHO"); }); + modelBuilder.Entity(entity => + { + entity.HasNoKey(); + + entity.ToView("VWMY_CATALOG"); + + entity.Property(e => e.Guid).HasColumnName("GUID"); + entity.Property(e => e.AddedWho) + .HasMaxLength(30) + .IsUnicode(false) + .HasColumnName("ADDED_WHO"); + entity.Property(e => e.AddedWhen) + .HasColumnType("datetime") + .HasColumnName("ADDED_WHEN"); + entity.Property(e => e.CatString) + .HasMaxLength(900) + .IsUnicode(false) + .HasColumnName("CAT_STRING"); + entity.Property(e => e.CatTitle) + .HasMaxLength(100) + .IsUnicode(false) + .HasColumnName("CAT_TITLE"); + entity.Property(e => e.ChangedWhen) + .HasColumnType("datetime") + .HasColumnName("CHANGED_WHEN"); + entity.Property(e => e.ChangedWho) + .HasMaxLength(30) + .IsUnicode(false) + .HasColumnName("CHANGED_WHO"); + }); + OnModelCreatingPartial(modelBuilder); } diff --git a/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs b/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs index 12f5e0e..e8e2c01 100644 --- a/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs +++ b/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs @@ -9,5 +9,6 @@ public class CatalogInfrastructureProfile : Profile public CatalogInfrastructureProfile() { CreateMap().ReverseMap(); + CreateMap(); } } diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index 2e69fde..db74c3e 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -21,14 +21,14 @@ public class CatalogRepository : ICatalogRepository public async Task> GetAllAsync(CancellationToken cancellationToken = default) { - var entities = await _db.TbmyCatalogs.AsNoTracking().ToListAsync(cancellationToken); - return _mapper.Map>(entities); + var viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken); + return _mapper.Map>(viewRows); } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { - var entity = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); - return entity == null ? null : _mapper.Map(entity); + var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); + return viewRow == null ? null : _mapper.Map(viewRow); } public async Task AddAsync(Catalog catalog, CancellationToken cancellationToken = default) diff --git a/DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs b/DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs new file mode 100644 index 0000000..973a885 --- /dev/null +++ b/DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs @@ -0,0 +1,18 @@ +namespace DbFirst.Infrastructure.ScaffoldEntities; + +public partial class VwmyCatalog +{ + public int Guid { get; set; } + + public string CatTitle { get; set; } = null!; + + public string CatString { get; set; } = null!; + + public string AddedWho { get; set; } = null!; + + public DateTime AddedWhen { get; set; } + + public string? ChangedWho { get; set; } + + public DateTime? ChangedWhen { get; set; } +}