Base solution for your next web application
Open Closed

Adding custom method to repositories (bulk insert extention) #7446


User avatar
0
robrechtbelien created

Hi, I'm trying to add a custom method to all my repositories. So in the baserepository code I added this:

    public abstract class AppRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<AppDbContext, TEntity, TPrimaryKey>
        where TEntity : class, IEntity<TPrimaryKey>
    {
        protected AppRepositoryBase(IDbContextProvider<AppDbContext> dbContextProvider)
            : base(dbContextProvider)
        {

        }

        //add your common methods for all repositories

        public async Task BulkInsert(List<TEntity> entities)
        {
            await Context.BulkInsertAsync(entities);
        }

    }

That is ok, but when trying to use it in a repository : IRepository&lt;classname, long> the method is not available.

Am I missing something?


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @robrechtbelien

    Instead of adding this method to your base repository class, add it as an extension method to IRepository interface.

  • User Avatar
    0
    robrechtbelien created

    Hi @ismcagdas,

    Can you point me in the right direction? Is the IRepository part of the ABP framework or is it in the aspnetzero code?

  • User Avatar
    0
    robrechtbelien created

    Additionally: When using the bulkinsert method, the entityhelper function are not run, is it possible to extend them to intercept the bulkinsert function somehow?

  • User Avatar
    0
    ismcagdas created
    Support Team

    I mean something like this https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Domain/Repositories/RepositoryExtensions.cs#L105

    Which library are you using for BulInsert ? I think it saves records in an unusual way and EF Core's savechanges can't see the changes in entities.