Base solution for your next web application
Open Closed

Migration's seed method and IRepository #2463


User avatar
0
edvin created

Hi,

is it possible to use IRepository<OrganizationUnit> inside custom context seed method. I create module with "organization unit dependency". So i wan't to seed some data into organizationunit entity from my module.

Is it possible and how ?

Thanks for advance.

Edvin


5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    No, you can not use repositories in db seed. Because, seed is used by EF migration tool and this tool does not initialize ABP and modules. So, there is no dependency injection or other tools.

    Seed method should directly work with database and should seed raw data. If you want, you can create some seed code in another project and call it from the seed method.

  • User Avatar
    0
    edvin created

    Thanks.

    I try add DBSet<OrganizationUnit> to my module context and add raw data, But this is not good idea because my context using different schema (not dbo).

    Then i try to derive my module context from AbpZeroDbContext. This still don't work because i use different schema name (not dbo).

    Finally i decide to create another DbContext class derived from AbpZeroCommonDbContext<Role, User> just for seeding raw data through it.

    Is this correct or there is another more clean solution ?

    Edvin

  • User Avatar
    0
    edvin created

    Creating another DBContext derived from AbpZeroCommonDbContext<Role, User> is not good idea.

    Now i get error

    AbpException: Found more than one concrete type for given DbContext Type (Abp.Zero.EntityFramework.AbpZeroCommonDbContext

    when starting project.

    I'm stucked.

    Hikalkan, please advice.

    Edvin

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can create your second DbContext like this one <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/blob/master/MultipleDbContextDemo/MultipleDbContextDemo.EntityFramework/EntityFramework/MySecondDbContext.cs">https://github.com/aspnetboilerplate/as ... Context.cs</a>.

    You can also investigate the example project.

  • User Avatar
    0
    edvin created

    Hi. Thanks for your tip. But this was not problem.

    In my module i already created 2 contexts:

    • first: for my entities - derived from AbpDbContext
    • second: for existing entities (users, organization units) - derived from AbpZeroDbContext<TUser, TRole .....

    Of course both are register to DI.

    When starting project i got error:

    _AbpException: Found more than one concrete type for given DbContext Type (Abp.Zero.EntityFramework.AbpZeroCommonDbContext`2[Sebit.CIS.Authorization.Roles.Role,Sebit.CIS.Authorization.Users.User]) define MultiTenancySideAttribute with Host. Found types: Sebit.CIS.EntityFramework.CISDbContext, Sebit.CIS.EntityFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, Sebit.BPM.EF.BPMZeroDbContext, Sebit.BPM.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.

    Abp.EntityFramework.DbContextTypeMatcher.GetDefaultDbContextType(List<Type> dbContextTypes, Type sourceDbContextType, MultiTenancySides tenancySide) Abp.EntityFramework.Uow.EfUnitOfWork.GetOrCreateDbContext

    When starting ZERO application it seems that ABP found 2 DBContexts that match for required Repository: One from

    Problem is solved now ! I remove my second context and just use existsing context from

    Thanks for help.

    Edvin