c# - How do I configue StructureMap to create a DbConnection -


i have code below entity framework context.

i using overloaded constructor inject in in memory database testing. works fine when use in mvc app need configure structuremap dbconnection. don't know how this

public class efcontext : dbcontext {     //this can full blown connection string or if single string defaulting sql express     public efcontext() : base("sqlexpresspaxiummusic")     {      }      public efcontext(dbconnection connection) : base(connection, true)     {      }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         database.setinitializer(new dbcontextinitialiser());     }      public dbset<user> users { get; set; } }      public static icontainer initialize()     {         objectfactory.configure(config =>         {             config.scan(scan =>             {                 scan.thecallingassembly();                 scan.withdefaultconventions();             });              config.for<iwebauthenticator>().use<webauthenticator>();             config.for<efcontext>().use<efcontext>();             config.for<iuserrepository>().use<userrepository>();             config.for<dbconnection>() ****what goes here**** ????         });          return objectfactory.container;     } 

you should tell structuremap efcontext constructor want use. structuremap default use greediest constructor, is, 1 parameters, in case public efcontext(dbconnection connection). @ runtime, want specify constructor public efcontext(), create dbconnection connectionstring:

config.for<efcontext>().use<efcontext>().selectconstructor(() => new efcontext()); 

as aside, use of objectfactory deprecated, , recommended not use it. see http://structuremap.github.io/integrations/aspnet-mvc/ recommended way setup structuremap inside asp.net mvc application.

edit: use correct selectconstructor syntax, because forgot...


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -