微服务数据库配置、测试与事件溯源技术解析
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
{