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.
This commit is contained in:
@@ -11,6 +11,7 @@ public partial class ApplicationDbContext : DbContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
public virtual DbSet<TbmyCatalog> TbmyCatalogs { get; set; }
|
public virtual DbSet<TbmyCatalog> TbmyCatalogs { get; set; }
|
||||||
|
public virtual DbSet<VwmyCatalog> VwmyCatalogs { get; set; }
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
@@ -49,6 +50,37 @@ public partial class ApplicationDbContext : DbContext
|
|||||||
.HasColumnName("CHANGED_WHO");
|
.HasColumnName("CHANGED_WHO");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<VwmyCatalog>(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);
|
OnModelCreatingPartial(modelBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ public class CatalogInfrastructureProfile : Profile
|
|||||||
public CatalogInfrastructureProfile()
|
public CatalogInfrastructureProfile()
|
||||||
{
|
{
|
||||||
CreateMap<TbmyCatalog, Catalog>().ReverseMap();
|
CreateMap<TbmyCatalog, Catalog>().ReverseMap();
|
||||||
|
CreateMap<VwmyCatalog, Catalog>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,14 +21,14 @@ public class CatalogRepository : ICatalogRepository
|
|||||||
|
|
||||||
public async Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default)
|
public async Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entities = await _db.TbmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
var viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
|
||||||
return _mapper.Map<List<Catalog>>(entities);
|
return _mapper.Map<List<Catalog>>(viewRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entity = await _db.TbmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
|
||||||
return entity == null ? null : _mapper.Map<Catalog>(entity);
|
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default)
|
||||||
|
|||||||
18
DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs
Normal file
18
DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs
Normal file
@@ -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; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user