Hell o,
I have a problem to register a repository with unity container. I have a following configuration and classes:
/* Generic repository: */
public class Repository<E> : IRepository<E> where E : EntityObject
{
private readonly ObjectContext _ctx;
public ObjectContext Context
{
get { return _ctx; }
}
public Repository(ObjectContext context)
{
_ctx = context;
}
}
/* Concrete repository: */
public class SourceRepository : Repository<Source>
{
public SourceRepository(EntityContext context) : base(context) { }
}
/* EF generated context: */
public partial class EntityContext : ObjectContext
{
public EntityContext() : base("name=EntityContext", "EntityContext")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
public EntityContext(string connectionString) : base(connectionString, "EntityContext")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
public EntityContext(EntityConnection connection) : base(connection, "EntityContext")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
}
/* Registration of EntityContext type with constructor without parameters */
<register type="ObjectContext" mapTo="EntityContext" name="EntityContext">
<constructor/>
</register>
/* Registration for generic repository (I'm not sure if this registration is needed) */
<register type="IRepository[]" mapTo="Repository[]" name="Repository">
<lifetime type="transient" />
<constructor>
<param name="context">
<dependency name="EntityContext"/>
</param>
</constructor>
</register>
/* Registration for concrete repository. I want to inject constructor dependency of EntityContext type */
<register type="IRepository[Source]" mapTo="SourceRepository" name="SourceRepository">
<lifetime type="transient" />
<constructor>
<param name="context">
<dependency name="EntityContext"/>
</param>
</constructor>
</register>
When I'm trying to load configuration I'm getting error:
Resolution of the dependency failed, type = "BL.DataAccess.Repository.SourceRepository", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type ObjectContext has multiple constructors of length 1. Unable to disambiguate.
I understand what this exception means, but I don't know where I have an error in my configuration.
Can you help please?
thanks.