在 aspnetboilerblate 中注入自定义存储库

Injecting custom repository in aspnetboilerblate

我正在尝试设置自定义存储库,因为我有一个基于全文搜索的查询。但是尝试设置自定义存储库会引发异常。看起来我没有正确连接依赖项。这是代码

    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 设置的方法

IocManager.RegisterAssemblyByConvention(typeof(PropertySearchEntityFrameworkModule).GetAssembly());     
  IocManager.IocContainer.Register(Component.For(typeof(IPropertySearchRepository<,>))
                    .ImplementedBy(typeof(PropertySearchRepositoryBase<,>))
                    .LifeStyle.Transient);

但我遇到异常

System.MissingMethodException occurred HResult=0x80131513
Message=Constructor on type '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]]' not found. Source=
StackTrace: at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)

不确定我错过了什么。

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)
    {
    }

    // ...
}

实现应该是这样的。

public interface ICustomRepository : IRepository<Custom, int>
{

}

public class CustomRepository : CustomRepositoryBase<Custom, int>, ICustomRepository
{
public CustomRepository(IDbContextProvider<CustomDbContext> dbContextProvider,
        IObjectMapper objectMapper)
    : base(dbContextProvider, objectMapper)
    {
    }
}