how to use default contructor

Jun 26, 2012 at 1:17 PM

my config file like this:

<alias alias="IGenericRepository" type="`1,"/>
    <alias alias="Repository" type="`1,"/>

 <alias alias="IPianoAreaService" type="PianoBusiness.BaseInfo.IPianoAreaService,PianoBusiness"/>
    <alias alias="PianoAreaService" type="PianoBusiness.BaseInfo.Impl.PianoAreaService,PianoBusiness"/>

<register type ="IGenericRepository" mapTo="Repository" name="pianoAreaDal"/>

<register type="IPianoAreaService" mapTo="PianoAreaService" name="pianoAreaService">
          <param name="pianoAreaDal">
            <dependency name="pianoAreaDal"></dependency>

and my service contructor like this:

public class PianoAreaService : IPianoAreaService

private Repository<PianoArea> _pianoAreaDal;

        public PianoAreaService(Repository<PianoArea> pianoAreaDal)
            _pianoAreaDal = pianoAreaDal;


public class Repository<T> : IGenericRepository<T> where T : class


private DbContext ctx;

/// <summary>
        /// Default constructor creating a new DbContext and setting Autosave to true
        /// </summary>
        public Repository()
            ctx = new DbContext(ConfigurationManager.AppSettings["EntityFrameworkGenericRepository_ConnectionString"]);
            ctx.Configuration.ProxyCreationEnabled = false;
            ctx.Configuration.ValidateOnSaveEnabled = true;
            Autosave = true;
            AutoDisposeContext = true;

/// <summary>
        /// Constructor specifying the DbContext to use for operations, and setting Autosave to false.
        /// </summary>
        /// <param name="context">The DbContext to use for operations</param>
        public Repository(DbContext context)
            ctx = context;
            ctx.Configuration.ProxyCreationEnabled = false;
            Autosave = true;
            AutoDisposeContext = false;


when i run test throw expection :

Resolution of the dependency failed, type = "PianoBusiness.BaseInfo.IPianoAreaService", name = "pianoAreaService".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type DbConnection cannot be constructed. You must configure the container to supply this value.
At the time of the exception, the container was:

  Resolving PianoBusiness.BaseInfo.Impl.PianoAreaService,pianoAreaService (mapped from PianoBusiness.BaseInfo.IPianoAreaService, pianoAreaService)
  Resolving parameter "pianoAreaDal" of constructor PianoBusiness.BaseInfo.Impl.PianoAreaService(`1[[PianoData.PianoArea, PianoData, Version=, Culture=neutral, PublicKeyToken=null]] pianoAreaDal)
    Resolving parameter "context" of constructor`1[[PianoData.PianoArea, PianoData, Version=, Culture=neutral, PublicKeyToken=null]](System.Data.Entity.DbContext context, System.Boolean validateEntitesOnSave)
      Resolving System.Data.Entity.DbContext,(none)
      Resolving parameter "existingConnection" of constructor System.Data.Entity.DbContext(System.Data.Common.DbConnection existingConnection, System.Data.Entity.Infrastructure.DbCompiledModel model, System.Boolean contextOwnsConnection)
        Resolving System.Data.Common.DbConnection,(none)

how to do ?

I want to use Repository contructor with no param!


Jun 26, 2012 at 1:21 PM

Just use an empty <constructor/> element in the configuration.

Jun 27, 2012 at 12:33 AM

I have resolved it