Why Since Adding Effort.EF6 Unit Tests can I no longer run my C# MVC Website?


Question

My C# MVC Website uses StructureMap for Dependency Injection.

The constructor for my DbContext used to be as follows.

public class PortalEntityModel : DbContext
{
    public PortalEntityModel() : base("name=PortalEntityModel")
    {
    }
}

Now I have added Effort.EF6 in order to unit test, I had to change my DbContext constructor to this.

public class PortalEntityModel : DbContext
{
    public PortalEntityModel(DbConnection connection) : base(connection, true)
    {
    }
}

While I can now unit test my code. I can't run the website anymore because StructureMap cannot create since the change.

No default Instance is registered and cannot be automatically determined for type 'System.Data.Common.DbConnection'

How can I tell StructureMap how to initialise my DBContext as before, while allowing Effort.EF6 to also work?

EDIT --

StructureMap is started when the website is started as follows...

    public static void Start()
    {
        IContainer container = IoC.Initialize();
        StructureMapDependencyScope = new StructureMapDependencyScope(container);
        DependencyResolver.SetResolver(StructureMapDependencyScope);
        DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
    }

It is started automatically when the website starts

[assembly: PreApplicationStartMethod(typeof(StructuremapMvc), "Start")]
[assembly: ApplicationShutdownMethod(typeof(StructuremapMvc), "End")]

Popular Answer

I managed to figure it out. When changing the constructor for the DBContext structuremap was unable to pass it a DbConnection.

So I have both constructors now and added a bit of logic to the structuremap setup that tells it which constructor the website should use.

c.For().Use().SelectConstructor(() => new PortalEntityModel());





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why