[英]Using Heroku PostgreSql in ASP .Net Core 3.0
I have got my web api application deployed on Heroku, which should remember and operate (increment/decrement) certain numbers.我已经在 Heroku 上部署了我的 web api 应用程序,它应该记住并操作(递增/递减)某些数字。 I thought it will be a good idea to keep those numbers in database.
我认为将这些数字保存在数据库中是个好主意。 Since this is my hobby project, I chose to connect Heroku PosgreSql add-on with free plan to my app.
由于这是我的爱好项目,因此我选择将具有免费计划的 Heroku PosgreSql 附加组件连接到我的应用程序。 So here is the question: how exactly do I connect my app to this database?
那么问题来了:我究竟如何将我的应用程序连接到这个数据库? I have seen a lot of tutorials, but each toturial was either too overloaded with code (and little explanations to it), or described connection to local PostgreSql database (which is not what I need).
我看过很多教程,但每个教程要么是代码过多(并且几乎没有解释),要么描述了与本地 PostgreSql 数据库的连接(这不是我需要的)。 I haven't do anything to this add-on except adding it to my app on app page.
除了将它添加到我的应用程序页面上的应用程序之外,我没有对此附加组件做任何事情。
Here is my Startup.cs (ConfigureServices method).这是我的 Startup.cs(ConfigureServices 方法)。 ConnectionString, which I got from Postgre add-on, is located in appsettings.json.
ConnectionString 是我从 Postgre 附加组件获得的,位于 appsettings.json 中。
Here is DatabaseContext.cs.这是 DatabaseContext.cs。
DbService class contains methods for work with database (create new entry, edit existing, delete, etc). DbService 类包含用于处理数据库的方法(创建新条目、编辑现有条目、删除等)。
I am new to web-api and databases, so I'm sorry if this question is dumb.我是 web-api 和数据库的新手,所以如果这个问题很愚蠢,我很抱歉。
Heroku provide DATABASE_URL environment variable with connection to PostgreSQL. Heroku 提供 DATABASE_URL 环境变量以连接到 PostgreSQL。 You need to transform provided url to efcore format
$"Server={pgHost};Port={pgPort};User Id={pgUserId};Password={pgPassword};Database={pgDatabase}";
您需要将提供的 url 转换为 efcore 格式
$"Server={pgHost};Port={pgPort};User Id={pgUserId};Password={pgPassword};Database={pgDatabase}";
. .
Please check this topic for more info and examples https://github.com/jincod/dotnetcore-buildpack/issues/33请查看此主题以获取更多信息和示例https://github.com/jincod/dotnetcore-buildpack/issues/33
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.