failed to register IUnitOfWork using containerRegistry #2330
-
Hello, i tried using prism to register the same interface this time with containerRegistry.unfortunatly there is no AddDatabase method, so i tried the following but i get an error : ResolutionFailedException: Resolution failed with error: No public constructor is available for type Microsoft.EntityFrameworkCore.DbContextOptions. thank you for the help ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@ErwinDraconis while Prism's container can generally resolve concrete types there are a few things that you probably want to adjust here... First of all, unless you're a DI guru, you really want to avoid using Scoped Services. That is a VERY advanced topic. Remember that Microsoft.Extensions.DependencyInjection was purpose built for ASP.NET where Scoped lifetimes make a lot of sense as each request represents a different user. In the Desktop or Mobile world, you have a single user, and more than likely want a Singleton or a Transient. For your IUnitOfWork and IUserRepository you probably want RegisterSingleton not RegisterScoped. By not registering the concrete DatabaseContext you are implicitly registering it as a Transient Service, or in other words each time it is resolved you will get a brand new instance which may or may not be good for your purposes. However you have a second and more important issue which is what is leading to the error that you see here. By providing no registrations there you haven't told it how to construct the DbContextOptions which you undoubtedly have as a constructor argument in your DatabaseContext. Essentially you need to create a DbContextOptionsBuilder where you would use the UseSqlite extension and then register the options instance from the builder. |
Beta Was this translation helpful? Give feedback.
@ErwinDraconis while Prism's container can generally resolve concrete types there are a few things that you probably want to adjust here...
First of all, unless you're a DI guru, you really want to avoid using Scoped Services. That is a VERY advanced topic. Remember that Microsoft.Extensions.DependencyInjection was purpose built for ASP.NET where Scoped lifetimes make a lot of sense as each request represents a different user. In the Desktop or Mobile world, you have a single user, and more than likely want a Singleton or a Transient. For your IUnitOfWork and IUserRepository you probably want RegisterSingleton not RegisterScoped.
By not registering the concrete DatabaseContext you are imp…