c# - How to inject DbContext implementation into Prism ViewModel constructor? -


i'm working on wpf prism application using unity , entity framework. i'm looking inject custom dbcontext while constructing viewmodels, can use through life of viewmodel. module dbcontext class in gets registered in bootstrapper, , dbcontext type gets registered during module.initialize().

with said, runtime error getting thrown when trying construct viewmodels inject dbcontext. it's saying idbconnection needs registered. when register module says dbconnection abstract class , can't constructed. there must i'm not aware of concerning how dbcontext supposed registered.

here's type being registered in module:

public class sharedresourcesmodule : imodule {     iunitycontainer container;       public sharedresourcesmodule(iunitycontainer container)     {         this.container = container;     }       public void initialize()     {         container.registertype<iauthenticationservice, authenticationservice>();         container.registertype<idatabasecontext, efdbcontext>();     } } 

and example of viewmodel constructor:

public class productcontextresultviewmodel : viewmodelbase {     public productcontextresultviewmodel(idatabasecontext databasecontext)     {         this.databasecontext = databasecontext efdbcontext;     }       private efdbcontext databasecontext { get; set; } } 

if there's other detail can provide please let me know!

exception detail below:

microsoft.practices.servicelocation.activationexception unhandled user code hresult=-2146233088 message=activation error occurred while trying instance of type object, key "productcontextresultview" source=microsoft.practices.servicelocation stacktrace: @ microsoft.practices.servicelocation.servicelocatorimplbase.getinstance(type servicetype, string key) in c:\projects\commonservicelocator\main\microsoft.practices.servicelocation\servicelocatorimplbase.cs:line 53 @ microsoft.practices.servicelocation.servicelocatorimplbase.getinstance[tservice](string key) in c:\projects\commonservicelocator\main\microsoft.practices.servicelocation\servicelocatorimplbase.cs:line 103 @ microsoft.practices.prism.regions.regionnavigationcontentloader.createnewregionitem(string candidatetargetcontract) innerexception: microsoft.practices.unity.resolutionfailedexception hresult=-2146233088 message=resolution of dependency failed, type = "system.object", name = "productcontextresultview". exception occurred while: while resolving.

exception is: invalidoperationexception - current type, system.data.common.dbconnection, abstract class , cannot constructed. missing type mapping?

at time of exception, container was:

resolving contextresultspane.productcontextresult.productcontextresultview,productcontextresultview (mapped system.object, productcontextresultview) resolving parameter "viewmodel" of constructor contextresultspane.productcontextresult.productcontextresultview(contextresultspane.productcontextresult.productcontextresultviewmodel viewmodel) resolving contextresultspane.productcontextresult.productcontextresultviewmodel,(none) resolving parameter "databasecontext" of constructor contextresultspane.productcontextresult.productcontextresultviewmodel(sharedresources.dataaccess.idatabasecontext databasecontext) resolving sharedresources.dataaccess.efdbcontext,(none) (mapped sharedresources.dataaccess.idatabasecontext, (none)) resolving parameter "connection" of constructor sharedresources.dataaccess.efdbcontext(system.data.common.dbconnection connection) resolving system.data.common.dbconnection,(none)

   source=microsoft.practices.unity    namerequested=productcontextresultview    typerequested=object    stacktrace:         @ microsoft.practices.unity.unitycontainer.dobuildup(type t, object existing, string name, ienumerable`1 resolveroverrides)         @ microsoft.practices.unity.unitycontainer.dobuildup(type t, string name, ienumerable`1 resolveroverrides)         @ microsoft.practices.unity.unitycontainer.resolve(type t, string name, resolveroverride[] resolveroverrides)         @ microsoft.practices.prism.unityextensions.unityservicelocatoradapter.dogetinstance(type servicetype, string key)         @ microsoft.practices.servicelocation.servicelocatorimplbase.getinstance(type servicetype, string key) in c:\projects\commonservicelocator\main\microsoft.practices.servicelocation\servicelocatorimplbase.cs:line 49    innerexception: system.invalidoperationexception         hresult=-2146233079         message=the current type, system.data.common.dbconnection, abstract class , cannot constructed. missing type mapping?         source=microsoft.practices.unity         stacktrace:              @ microsoft.practices.objectbuilder2.dynamicmethodconstructorstrategy.throwforattemptingtoconstructabstractclass(ibuildercontext context)              @ lambda_method(closure , ibuildercontext )              @ microsoft.practices.objectbuilder2.dynamicbuildplangenerationcontext.<>c__displayclass1.<getbuildmethod>b__0(ibuildercontext context)              @ microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey)              @ microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context)              @ lambda_method(closure , ibuildercontext )              @ microsoft.practices.objectbuilder2.dynamicbuildplangenerationcontext.<>c__displayclass1.<getbuildmethod>b__0(ibuildercontext context)              @ microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey)              @ microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context)              @ lambda_method(closure , ibuildercontext )              @ microsoft.practices.objectbuilder2.dynamicbuildplangenerationcontext.<>c__displayclass1.<getbuildmethod>b__0(ibuildercontext context)              @ microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey)              @ microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context)              @ lambda_method(closure , ibuildercontext )              @ microsoft.practices.objectbuilder2.dynamicbuildplangenerationcontext.<>c__displayclass1.<getbuildmethod>b__0(ibuildercontext context)              @ microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context)              @ microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context)              @ microsoft.practices.unity.unitycontainer.dobuildup(type t, object existing, string name, ienumerable`1 resolveroverrides)         innerexception:  

glen thomas' guidance helped point me doing more reading, , turns out unity uses verbose constructor default. core of problem, since had constructor required dbconnection passed unit testing.

in order force unity use default constructor changed this...

container.registertype<idatabasecontext, efdbcontext>(); 

...to this...

container.registertype<idatabasecontext, efdbcontext>(new injectionconstructor()); 

after change it's working intended!


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -