Starts in:
0 DAY
17 HRS
35 MIN
10 SEC
Starts in:
0 D
17 H
35 M
10 S
Open Closed

Entity framework core SetDefaultRepository #2518


User avatar
0
Kaspars.Avotins created

Hello,

Could you explain how SetDefaultRepository functionality is supposed to work. For me SetDefaultRepository seems to do nothing. (Tried this in different projects and also tried this on Acme.BookStore tutorial project)

How do I create/set default repository for all entities in dbContext?

Looking at documentation I create classes MyRepositoryBase<TEntity> and MyRepositoryBase<TEntity, TKey>. Then following documentation set them with SetDefaultRepository. context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); }); Base class MyRepositoryBase is ignored.

I get expected result when I set base class for specific entity context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.AddRepository<Authors.Author, MyRepositoryBase<Authors.Author, Guid>>(); });

Am I not misunderstanding something? I would like to set base class for all entities. How do I accomplish this?


5 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    It registers your custom class as IRepository<> and IBasicRepository<>. So, if some class injects one of them, your custom class will be used.

    The following Unit Test should explain clearly: https://github.com/abpframework/abp/blob/e3e1779de6df5d26f01cdc8e99ac9cbcb3d24d3c/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs#L169

  • User Avatar
    0
    Kaspars.Avotins created

    It registers your custom class as IRepository<> and IBasicRepository<>. So, if some class injects one of them, your custom class will be used.

    The following Unit Test should explain clearly: https://github.com/abpframework/abp/blob/e3e1779de6df5d26f01cdc8e99ac9cbcb3d24d3c/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs#L169

    UnitTest shows that class is indeed registered.

    But same happens to my projects, class is registered but not injected. For example we will have two entities Author and Book and context BookStoreDbContext.

    Example 1:

    context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options .AddDefaultRepositories<BookStoreDbContext>(true) .SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); }

    If we look through registered services we can find that MyRepositoryBase was indeed registered for each entity. (There is 8 registered instances for each entity with interfaces IReadOnlyBasicRepository, IReadOnlyRepository, IBasicRepository, IRepository with and without key)

    BUT when I inject IRepository<Author, Guid> my base repository MyRepositoryBase is ignored.

    Example 2:

    context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.AddRepository<Author, MyRepositoryBase<Author, Guid>>(); }

    If we look through registered services we can find that MyRepositoryBase was indeed registered for entity Author.

    When I inject IRepository<Author, Guid> eveything works as expected. MyRepositoryBase is not ignored and is injected.

    Example 3:

    context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options .AddDefaultRepositories<BookStoreDbContext>(true) .SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); } options.AddRepository<Book, MyRepositoryBase<Book, Guid>>(); is converted to options.AddRepository<>();

    Same as before MyRepositoryBase registered for every entity.

    When I inject IRepository<Author, Guid> my base repository MyRepositoryBase is ignored. When I inject IRepository<Book, Guid> eveything works as expected. MyRepositoryBase is not ignored and is injected.


    Testing done on ABP Framework version: 4.4.3 and 5.0.1

    As I told before I do not understand how I am supposed to set up base repository.

    Could you provide working example project with ef core where SetDefaultRepositoryClasses is working. Or some workaround? Or explanation what I am not understanding.

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    How did you implement MyRepositoryBase ?

    Is it extended from EfCoreRepository ?

  • User Avatar
    0
    Kaspars.Avotins created

    MyRepositoryBase<TEntity> : EfCoreRepository<BookStoreDbContext, TEntity> where TEntity : class, IEntity and MyRepositoryBase<TEntity, TKey> : EfCoreRepository<BookStoreDbContext, TEntity, TKey> where TEntity : class, IEntity<TKey>

    also tried to extend RepositoryBase directly. Nothing worked for me and as I stated in examples everything works fine when using AddRepository for specific entity.

    If you can provide me with simple ef core example project where SetDefaultRepositoryClasses is working it would be great. With actual implementation since MyRepositoryBase gets registered to services, but with wrong state or something. Because it is not injected and is ignored.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi Kaspars

    Please share your example project with me, I will check it. liming.ma@volosoft.com

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06