How do I correctly reference a database context in one project from an API in another project, without duplicating the ConnectionString configuration? #1525
Replies: 4 comments 4 replies
|
Can you show the entire AppHost project's Program.cs and the application's Program.cs (where the DbContext wire up is happening?) |
|
Hi David Fowler (@davidfowl), the AppHost is https://github.com/Project-KONGOR-Open-Source/ASPIRE/blob/main/ASPIRE.AppHost/ASPIRE.cs and the database project is https://github.com/Project-KONGOR-Open-Source/ASPIRE/blob/main/MERRICK.Database/MERRICK.cs. Essentially, what I'm trying to achieve is to give https://github.com/Project-KONGOR-Open-Source/ASPIRE/blob/main/ZORGATH.WebPortal.API/ZORGATH.cs access to the database context without duplicating the connection string in the configuration, if that is possible. |
|
Also, thank you very much for the prompt response! |
|
I don't know how to do it with Aspiere, but you can add a key vault and store the connection string there. The key vault can be added to the Aspiere host and injected into the other applications. |
Uh oh!
There was an error while loading. Please reload this page.
I've been following the eShopLite example but I can't figure out what is going on because when I try to inject my database context I keep getting
"ConnectionString is missing. It should be provided in 'ConnectionStrings:database' or under the 'ConnectionString' key in 'Aspire:Microsoft:EntityFrameworkCore:SqlServer' or 'Aspire:Microsoft:EntityFrameworkCore:SqlServer:DatabaseContext' configuration section.". This is (roughly) my setup.I've got a database manager project that registers a SQL Server service...
builder.AddSqlServerDbContext<MyContext>("MyConnectionStringName");...and everything works fine in that project.
The Aspire.AppHost builder looks like something along the lines of...
But if I register
MyContextas a dependency in my API project and try to inject it into a controller it doesn't quite work, and I'm not sure what I'm doing wrong. I was expecting that if I just dobuilder.AddSqlServerDbContext<MyContext>("MyConnectionStringName");in my API project then there is some magic that happens behind the scenes and the connection string is retrieved from some shared Aspire context.In the eShopLite sample solution I noticed that one project had some JSON config along the lines of...
...so I tried a placeholder connection string in my API project too, but that didn't work either.
Could someone please point me in the right direction, because I'm not sure what I'm doing wrong. I just want to reference my database context from my API project without duplicating the connection string. Is this possible?
All reactions