Call: +1 (917) 460-0063 - Or - +1 (876) 630-3807

The type DbConnection does not have an accessible constructor

Date: July 25, 2013 Author: Curtis Category: Coding Tags: , , , Comments: 0

Have you ever been moving along nicely in your development. Configured your IoC container using Microsoft Patterns & Practices Team Unity; created your Repository pattern; have your repositories setup nicely with a constructor signature making use of the DbContext class given to with Entity Framework from Microsoft. Have your Unit Tests setup as well mocking all your different repositories. All the tests are passing, and everything is looking nice.

So now your are ready for some UI and Integration testing, and then BAMB!! It hits you like a freight train going 200 MPH;  this exception is thrown from the Unity container: "The type DbConnection does not have an accessible constructor." And if you where using Prims like I was this would take some amount of Inner Exception digging to find the true cause of this exception.

Solution

Create an interface for DbContext and register that type matching with you concrete implementation of DbContext which is more that likely your own EF Database Context implementation. Like this:

Container.RegisterType<IDbContext,MyConcreteImplDbContext>();

Here is our implementation of the IDbContext interface.

// Copyright (c) Syntax Technology Solutions LLC.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================

namespace Syntax.Data.Interfaces
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Validation;

    public interface IDbContext
    {
        /// <summary>
        /// Creates a Database instance for this context that allows for creation/deletion/existence checks
        ///                 for the underlying database.
        /// 
        /// </summary>
        Database Database { get; }

        /// <summary>
        /// Provides access to features of the context that deal with change tracking of entities.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// An object used to access features that deal with change tracking.
        /// </value>
        DbChangeTracker ChangeTracker { get; }

        /// <summary>
        /// Provides access to configuration options for the context.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// An object used to access configuration options.
        /// </value>
        DbContextConfiguration Configuration { get; }

        /// <summary>
        /// Returns a DbSet instance for access to entities of the given type in the context,
        ///                 the ObjectStateManager, and the underlying store.
        /// 
        /// </summary>
        /// 
        /// <remarks>
        /// See the DbSet class for more details.
        /// 
        /// </remarks>
        /// <typeparam name="TEntity">The type entity for which a set should be returned.</typeparam>
        /// <returns>
        /// A set for the given entity type.
        /// </returns>
        DbSet<TEntity> Set<TEntity>() where TEntity : class;

        /// <summary>
        /// Returns a non-generic DbSet instance for access to entities of the given type in the context,
        ///                 the ObjectStateManager, and the underlying store.
        /// 
        /// </summary>
        /// <param name="entityType">The type of entity for which a set should be returned.</param>
        /// <returns>
        /// A set for the given entity type.
        /// </returns>
        /// 
        /// <remarks>
        /// See the DbSet class for more details.
        /// 
        /// </remarks>
        DbSet Set(Type entityType);

        /// <summary>
        /// Saves all changes made in this context to the underlying database.
        /// 
        /// </summary>
        /// 
        /// <returns>
        /// The number of objects written to the underlying database.
        /// </returns>
        /// <exception cref="T:System.InvalidOperationException">Thrown if the context has been disposed.</exception>
        int SaveChanges();

        /// <summary>
        /// Validates tracked entities and returns a Collection of <see cref="T:System.Data.Entity.Validation.DbEntityValidationResult"/> containing validation results.
        /// 
        /// </summary>
        /// 
        /// <returns>
        /// Collection of validation results for invalid entities. The collection is never null and must not contain null
        ///                 values or results for valid entities.
        /// 
        /// </returns>
        /// 
        /// <remarks>
        /// 1. This method calls DetectChanges() to determine states of the tracked entities unless
        ///                 DbContextConfiguration.AutoDetectChangesEnabled is set to false.
        ///                 2. By default only Added on Modified entities are validated. The user is able to change this behavior
        ///                 by overriding ShouldValidateEntity method.
        /// 
        /// </remarks>
        IEnumerable<DbEntityValidationResult> GetValidationErrors();

        /// <summary>
        /// Gets a <see cref="T:System.Data.Entity.Infrastructure.DbEntityEntry`1"/> object for the given entity providing access to
        ///                 information about the entity and the ability to perform actions on the entity.
        /// 
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam><param name="entity">The entity.</param>
        /// <returns>
        /// An entry for the entity.
        /// </returns>
        DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;

        /// <summary>
        /// Gets a <see cref="T:System.Data.Entity.Infrastructure.DbEntityEntry"/> object for the given entity providing access to
        ///                 information about the entity and the ability to perform actions on the entity.
        /// 
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>
        /// An entry for the entity.
        /// </returns>
        DbEntityEntry Entry(object entity);

        /// <summary>
        /// Calls the protected Dispose method.
        /// 
        /// </summary>
        void Dispose();

        [EditorBrowsable(EditorBrowsableState.Never)]
        string ToString();

        [EditorBrowsable(EditorBrowsableState.Never)]
        bool Equals(object obj);

        [EditorBrowsable(EditorBrowsableState.Never)]
        int GetHashCode();

        [EditorBrowsable(EditorBrowsableState.Never)]
         Type GetType();
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *