I am trying to setup a customrepository becuase I have a full text search based query. But trying to setup a custom repository is throwing exception. Looks like I have not properly wired up dependency. Here is the code
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);
}
}
And here is how I have done the di setup
IocManager.RegisterAssemblyByConvention(typeof(PropertySearchEntityFrameworkModule).GetAssembly());
IocManager.IocContainer.Register(Component.For(typeof(IPropertySearchRepository<,>))
.ImplementedBy(typeof(PropertySearchRepositoryBase<,>))
.LifeStyle.Transient);
But I am getting below exception
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)
Not sure what I am missing.
CustomRepositoryBase
cannot be abstract
and needs a public
constructor:
public class CustomRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<CustomDbContext, TEntity, TPrimaryKey>, ICustomRepository<TEntity,TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
public CustomRepositoryBase(IDbContextProvider<CustomDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
// ...
}
Implementation should be something like this.
public interface ICustomRepository : IRepository<Custom, int>
{
}
public class CustomRepository : CustomRepositoryBase<Custom, int>, ICustomRepository
{
public CustomRepository(IDbContextProvider<CustomDbContext> dbContextProvider,
IObjectMapper objectMapper)
: base(dbContextProvider, objectMapper)
{
}
}
User contributions licensed under CC BY-SA 3.0