Dependency injection in WPF
I'm trying to do DI in my WPF app. I've read about it on different sites and followed youtubers but it doesn't seem to work (it runs but I can't reach the registered service, it'll always be null).
I am basically trying to inject a DbContext as a Singleton because its scoped nature is causing some issues in the application (entity mismatching etc. = badly written code probably).
Help/Tips please!
13 Replies
I have to add that I am trying to reach the dbcontext from my repository
I made the dbcontext static from MainWindow and that did the trick. I don't like static though
GitHub
GitHub - SirRufo/so-77657479-PageNavigation: Example for stackoverflow
Example for stackoverflow. Contribute to SirRufo/so-77657479-PageNavigation development by creating an account on GitHub.
Use the
IDbContextFactory<TContext>
interface for injection
https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.infrastructure.idbcontextfactory-1?view=entity-framework-6.2.0IDbContextFactory Interface (System.Data.Entity.Infrastructure)
A factory for creating derived DbContext instances. Implement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as model rendering, DDL generation etc. To enable design-t...
GitHub
wpf/src/WpfWithAspNetCore at main · devdevdeveau/wpf
Contribute to devdevdeveau/wpf development by creating an account on GitHub.
Thank you guys. The links you sent do use DI but I don't see any DbContext stuff or maybe I'm blind. I'm not really that experienced so I can only assume I am unaware.
Also, I have just opened a few files (from links) and tried to understand them so I have not seen the full picture of things yet. It's getting late etc.
I also don't want a contextfactory for my WPF application. That would cause problems as the code is right now. I want to use the very same dbcontext so it can track all the entities correctly.
Oh, mine was a DI sample in WPF; there shouldn't be a special requirement for DbContext in wpf, can you show your $code?
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/You have two examples for WPF with DI container - what keeps you away from using the DbContext? There are thousands of tutorials on how to register a DbContext on the DI container and how to use that
To register your DbContext for DI, once you have the Microsoft.DependencyInjection libraries installed and working, as well as the Entity Framework Core libs installed, you just use
in your setup logic
Problematic issue is the connection string somehow has to be well known to your wpf app.
I just usually also install the Configuration packages from Microsoft and use IConfiguration stuff
Sure; that's fine
But still if you have to rotate connection string it can be hard
If you have to rotate any configuration it can be hard - even if you connect to a rest api ;o)
But you can keep the rest api config even when you change the underlying database