0

I have the code below for my Entity Framework context.

I am using the overloaded constructor to inject in an in memory database for testing. This works fine but when I use this in my MVC app I need to configure StructureMap for the DbConnection. I don't know how to do this

public class EfContext : DbContext
{
    //This can be a full blown connection string or if it is just a single string like this it is defaulting to 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;
    }
Dave Amour
  • 155
  • 1
  • 13
  • As explained in [this article](https://cuttingedge.it/blogs/steven/pivot/entry.php?id=97), you should not auto-wire framework types such as EF's `DbContext`. – Steven Jun 01 '15 at 10:11

1 Answers1

0

You should tell StructureMap which EfContext constructor you want to use. StructureMap will by default use the greediest constructor, that is, the one with the most parameters, in your case public EfContext(DbConnection connection). So at runtime, you want to specify the constructor public EfContext(), which will create the DbConnection from the connectionstring:

config.For<EfContext>().Use<EfContext>().SelectConstructor(() => new EfContext());

As an aside, the use of ObjectFactory is deprecated, and it is very strongly recommended not to use it. See http://structuremap.github.io/integrations/aspnet-mvc/ for the recommended way to setup StructureMap inside an ASP.NET Mvc application.

Edit: Use correct SelectConstructor syntax, because I forgot...

Stuart Grassie
  • 3,043
  • 1
  • 27
  • 36
  • Thanks - needed to be like this though: `config.For().Use().SelectConstructor(() => new EfContext());` – Dave Amour Jun 01 '15 at 10:32
  • That link mentions MVC5 - I am using MVC4, should I still use the new way? – Dave Amour Jun 01 '15 at 10:34
  • Unless you can upgrade your project to MVC5, then https://www.nuget.org/packages/StructureMap.MVC4/ should be ok. It will setup the recommended usage. You'll probably have to tinker with it a little to get it fully working. I'd set it up in a new project just to see how what it will do, before you apply to your current project. That said, if what you has works... then you'd only need to change it at such time as `ObjectFactory` is actually removed from StructureMap. – Stuart Grassie Jun 01 '15 at 10:39