ASP.NET Core 中隐藏数据库的 Ids 可以通过使用视图模型和 AutoMapper 来实现。下面是一个示例解决方法。
public class ProductViewModel
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    // 其他需要传输的属性...
}
public void ConfigureServices(IServiceCollection services)
{
    // 其他配置...
    
    services.AddAutoMapper(typeof(Startup));
    
    // 其他配置...
}
public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap();
        // 其他映射关系...
    }
}
 public class ProductController : Controller
{
    private readonly IMapper _mapper;
    private readonly ApplicationDbContext _dbContext;
    public ProductController(IMapper mapper, ApplicationDbContext dbContext)
    {
        _mapper = mapper;
        _dbContext = dbContext;
    }
    public IActionResult Index()
    {
        var products = _dbContext.Products.ToList();
        var productViewModels = _mapper.Map>(products);
        
        return View(productViewModels);
    }
}
在上述示例中,数据库实体 Product 的 Ids 被隐藏,而视图模型 ProductViewModel 只包含需要传输的属性。使用 AutoMapper 进行实体到视图模型的映射,可以将数据库实体转换为视图模型,然后在控制器中将视图模型传递给视图进行显示。