You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
foreach(varctxingetContexts()){awaitCreatePolicy.ExecuteAsync(async()=>{awaitctx.Database.EnsureDeletedAsync();// This is the culpritawaitctx.Database.EnsureCreatedAsync();});}
Unfortunately, if you add multiple DbContexts, every one loop will delete and re-create the database (with different schema!) because we use single connection string. This makes it unusable in multi-context systems. There is a workaround that just overrides the registered context (in TestOverridesPostModule pattern), like:
Here is a rough idea on how we can address that systematically:
Project problem
Now, most of the projects depend on SqlServer__ConnectionString env variable even if it has multiple contexts in it. This is rather too much of a simplification - if we have multiple DbContexts in a project, each should be configured with a separate connection string. This would allow us to juggle databases with ConfigurationOverrides and use separate (physical) database for each and every contest.
CoreLibrary problem
The main culprit is here, in ConfigurationOverrides. It just assumes that it provides a single connection string. That is very limiting. Forcing overriding the addresses and log level is also limiting - maybe you don't want that or you have a different configuration scheme.
To fix that, we should generalize the overrides.
Proposed solution
Instead of having ConfigurationOverrides look like it looks now (so a predefined set of config vars), it should accept a set of variables as a dictionary and inject it at runtime. We don't rely on any dynamic behavior there (nor should we), so it will suffice. To handle the connection strings, we just need a static factory method - we already rely on env variable there, that is accessible using other static methods.
Of course, it needs tests and probably some DX improvements (builder pattern with two methods on ConfigurationOverrides, one for plain value, the other for connection strings). Plus some documentation in docs (we should use this issue to start documenting how we do integration testing).
Currently, we do this in
DbContextsInitializer
:Unfortunately, if you add multiple
DbContext
s, every one loop will delete and re-create the database (with different schema!) because we use single connection string. This makes it unusable in multi-context systems. There is a workaround that just overrides the registered context (inTestOverridesPostModule
pattern), like:but that is suboptimal.
The text was updated successfully, but these errors were encountered: