地狱邻多个构造,统一错误而载入配置:类型ObjectContext的具有长度1,无法消除歧义
我必须注册具有单位容器的储存库中的问题。我有以下配置和类:
/*通用存储库:*/
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;
}
}
/*混凝土存储库:*/
public class SourceRepository : Repository<Source>
{
public SourceRepository(EntityContext context) : base(context) { }
}
/* EF产生上下文:*/
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();
}
}
/*注册EntityContext类型与不带参数的构造函数*/
<register type="ObjectContext" mapTo="EntityContext" name="EntityContext">
<constructor/>
</register>
/*登记通用库(我不知道是否需要这个注册)*/
<register type="IRepository[]" mapTo="Repository[]" name="Repository">
<lifetime type="transient" />
<constructor>
<param name="context">
<dependency name="EntityContext"/>
</param>
</constructor>
</register>
/*登记具体资料库。我想注入的EntityContext型*的构造函数依赖/
<register type="IRepository[Source]" mapTo="SourceRepository" name="SourceRepository">
<lifetime type="transient" />
<constructor>
<param name="context">
<dependency name="EntityContext"/>
</param>
</constructor>
</register>
当我试图加载配置我收到提示:
依赖解析失败,TYPE =“BL.DataAccess。 Repository.SourceRepository“,name =”(none)“。 发生异常时:解析时。 异常是:InvalidOperationException - 类型ObjectContext具有长度为1的多个构造函数。无法消除歧义。
我明白这个异常是什么意思,但我不知道我的配置中有什么错误。
你能帮忙吗?
谢谢。
指定的EntityContext作为构造配置可能做的伎俩参数类型。 – GrantVS 2012-02-16 23:13:00
如果你的意思是这样的: <依赖NAME = “的EntityContext”/> 构造> 所以它并没有帮助。同样的错误。 –
zosim
2012-02-16 23:24:43
看起来SourceRepository只有一个约束器。你可以尝试Type t = typeof(SourceRepository); ConstructorInfo [] constructorInfos = t.GetConstructors();检查它有多少承包商。 – findcaiyzh 2012-02-16 23:30:54