构建 .NET Core 微服务与数据库交互:技术实践与设计模式探索
1. 配置文件覆盖与数据库上下文配置
在开发过程中,我们可以通过变量来覆盖配置文件提供的默认值。例如, appsettings.json 文件如下:
{
"transient": false,
"postgres": {
"cstr": "Host=localhost;Port=5432;Database=locationservice;Username=integrator;Password=inteword"
}
}
这种配置方式使得在部署环境中覆盖默认配置变得轻松,同时让开发人员在开发工作站上有较低的摩擦体验。
为了让之前构建的存储库正常工作,我们需要配置数据库上下文。数据库上下文是 Entity Framework Core 的核心原语。以下是创建位置模型数据库上下文的代码:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using StatlerWaldorfCorp.LocationService.Models;
using Npgsql.EntityFrameworkCore.PostgreSQL;
namespace StatlerWaldorfCorp.LocationService.Persistence
{
超级会员免费看
订阅专栏 解锁全文
1731

被折叠的 条评论
为什么被折叠?



