我正在尝试设置自定义存储库,因为我有一个基于全文搜索的查询。但是尝试设置自定义存储库会引发异常。看起来我没有正确连接依赖。这是代码
public interface ICustomRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
IQueryable<TEntity> FromSql(string rawSqlQuery);
}
public interface ICustomRepository<TEntity> : ICustomRepository<TEntity,int>
where TEntity : class, IEntity<int>
{
}
/// <summary>
/// Base class for custom repositories of the application.
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
/// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam>
public abstract class CustomRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<CustomDbContext, TEntity, TPrimaryKey>, ICustomRepository<TEntity,TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
protected CustomRepositoryBase(IDbContextProvider<CustomDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
// Add your common methods for all repositories
public IQueryable<TEntity> FromSql(string rawSqlQuery)
{
var dbContext = GetDbContext();
return dbContext.Set<TEntity>().FromSql(rawSqlQuery);
}
}
这就是我如何完成di setup
IocManager.RegisterAssemblyByConvention(typeof(PropertySearchEntityFrameworkModule).GetAssembly());
IocManager.IocContainer.Register(Component.For(typeof(IPropertySearchRepository<,>))
.ImplementedBy(typeof(PropertySearchRepositoryBase<,>))
.LifeStyle.Transient);
但我得到的是异常
发生System.MissingMethodException HResult = 0x80131513
Message =类型的构造函数 &#39; Custom.EntityFrameworkCore.Repositories.CustomRepositoryBase`2 [[Custom.CustomProperties.CustomProperty, Custom.Core,Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null],[System.Int64,System.Private.CoreLib, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e]]&#39; 未找到。来源=
StackTrace:在System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,Binder binder,Object [] args,CultureInfo culture, Object [] activationAttributes,StackCrawlMark&amp; stackMark)
不确定我错过了什么。
答案 0 :(得分:1)
CustomRepositoryBase
不能是abstract
,需要public
构造函数:
public class CustomRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<CustomDbContext, TEntity, TPrimaryKey>, ICustomRepository<TEntity,TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
public CustomRepositoryBase(IDbContextProvider<CustomDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
// ...
}
答案 1 :(得分:0)
实施应该是这样的。
public interface ICustomRepository : IRepository<Custom, int>
{
}
public class CustomRepository : CustomRepositoryBase<Custom, int>, ICustomRepository
{
public CustomRepository(IDbContextProvider<CustomDbContext> dbContextProvider,
IObjectMapper objectMapper)
: base(dbContextProvider, objectMapper)
{
}
}