umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.4k stars 2.66k forks source link

Issue loading data with an IEFCoreScopeProvider and an external database #16805

Closed NobleMule closed 1 month ago

NobleMule commented 1 month ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.4.1

Bug summary

We need to access data stored in a completely separate database so we add a new context using the AddUmbracoDbContext extension method and set the connection string in the options. This works as expected when running migrations to build the database from the models but it fails when attempting to retrieve data.

Specifics

image

We have two connection strings in the app settings: One pointing to a DB called UmbError (which holds all the Umbraco tables) and another pointing to UmbErrorExternal (an entirely separate database that will hold custom data - movies in our example)

image

We add the context for the external database in a composer that references our external DB connection string. As mentioned in the summary, creating the tables using migrations works as expected. However, when we try to use this context to retrieve data, the context always seems to have the connection string of the Umbraco database, causing Entity to fail to find our tables.

image

Shown above is our injection of IEFCoreScopeProvider<MovieContext> and an inspection of the injected object having the connection string for the UmbError table where we expect it to be UmbErrorExternal

Steps to reproduce

Sample code with clean Umbraco and minimal steps is available here: https://github.com/NobleMule/UmbExternalDBIssue

To reproduce manually:

  1. Create 2 databases: One for Umbraco, one for external data
  2. Create models to define the external table
  3. Create a context for the external database
  4. Add a composer to add the DB context and use a notification handler to run migrations
  5. Add code to run DB migrations
  6. Run to validate that both databases get populated with expected tables
  7. Add some data to the external table (optional)
  8. Add an API endpoint which will return data from the external table
  9. Call API endpoint in browser/postman/etc

Expected result / actual result

The expected result (if using the linked sample repo) is that the API endpoint at /api/movies/ returns a collection of movies and their genres

The actual result is we get an error stating that our table cannot be found (which is due to the connection string pointing to a different DB to the one we want

image

github-actions[bot] commented 1 month ago

Hi there @NobleMule!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

NobleMule commented 1 month ago

I have just noticed that a fellow Umbraco user is having the same issue: https://our.umbraco.com/forum/using-umbraco-and-getting-started/113814-gettting-data-from-external-database

Zeegaan commented 1 month ago

Hello there 👋 I just had a talk with the team about this.

For now, we don't support external database sources to be used with our Scopes, the regular IScope does not work with other databases either.

In the future, it would be really cool to support multiple data sources, but for now it is not an option 😁

You can however just use the DbContext as you would normally!

I will go ahead and mark this as a feature request 😁