Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Seed OUs #8443


User avatar
0
-bitman created

Hello ANZ,

I would like to ask if there is a way to seed organization units so I can assign them my default roles? I was able to create default roles on top of the Admin and User roles and I would want to escalate these default values and assignments to the OU.

Please advise.

Thank you!


11 Answer(s)
  • User Avatar
    0
    -bitman created

    I would appreciate any response to this. Thank you.

  • User Avatar
    0
    BobIngham created

    Hi ramilcatalandomingo, You could use SeedHelper in project EntityFrameworkCore to plug in your own class: Your DefaultTenantDataBuilder would look as follows:

    using Nuagecare.EntityFrameworkCore;
    
    namespace Nuagecare.Migrations.Seed.Tenants
    {
        public class DefaultTenantDataBuilder
        {
            private readonly NuagecareDbContext _context;
            private readonly int _tenantId;
    
    
            public DefaultTenantDataBuilder(
                NuagecareDbContext context,
                int tenantId)
            {
                _context = context;
                _tenantId = tenantId;
            }
    
            public void Create()
            {
                new InitialOrganizationUnitCreator(_context, _tenantId).Create();
                new InitialNcFormCreator(_context, _tenantId).Create();
                _context.SaveChanges();
                new InitialNcEntityCreator(_context, _tenantId).Create();
                new InitialDeviceCreator(_context, _tenantId).Create();
                _context.SaveChanges();
    
            }
    
            public void CreateChildTables(int tenantId)
            {
                //Nuagecare InitialDataCreators
                new InitialNcActionFirstLevelCreator(_context, tenantId).Create();
                _context.SaveChanges();
            }
        }
    }
    

    And then your InitialOrganizationUnitCreator something like this:

    using Abp.Dependency;
    using Abp.Domain.Uow;
    using Abp.Organizations;
    using Microsoft.EntityFrameworkCore;
    using Nuagecare.EntityFrameworkCore;
    using System.Linq;
    
    namespace Nuagecare.Migrations.Seed.Tenants
    {
        public class InitialOrganizationUnitCreator
        {
            private readonly NuagecareDbContext _context;
            private readonly int _tenantId;
    
            public InitialOrganizationUnitCreator(
                NuagecareDbContext context,
                int tenantId)
            {
                _context = context;
                _tenantId = tenantId;
            }
    
            public void Create()
            {
                var root = _context.OrganizationUnits.IgnoreQueryFilters().FirstOrDefault(m => m.TenantId == _tenantId && m.DisplayName == "Acme Care Home");
                if (root == null)
                {
                    using (var organizationUnitManager = IocManager.Instance.ResolveAsDisposable<OrganizationUnitManager>())
                    {
                        organizationUnitManager.Object.Create(new OrganizationUnit(_tenantId, "Acme Care Home", null));
                    }
                }
                _context.SaveChanges();
                root = _context.OrganizationUnits.IgnoreQueryFilters().FirstOrDefault(m => m.TenantId == _tenantId && m.DisplayName == "Acme Care Home");
    
                using (var scope = IocManager.Instance.CreateScope())
                {
                    var organizationUnitManager = scope.Resolve<OrganizationUnitManager>();
                    var unitOfWorkManager = scope.Resolve<IUnitOfWorkManager>();
    
                    using (unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant))
                    {
                        var admissions = _context.OrganizationUnits.IgnoreQueryFilters().FirstOrDefault(m => m.TenantId == _tenantId && m.DisplayName == "Admissions");
                        if (admissions == null)
                        {
                            organizationUnitManager.Create(new OrganizationUnit(_tenantId, "Admissions", root.Id));
                        }
    
                        var firstFloor = _context.OrganizationUnits.IgnoreQueryFilters().FirstOrDefault(m => m.TenantId == _tenantId && m.DisplayName == "First Floor");
                        if (firstFloor == null)
                        {
                            organizationUnitManager.Create(new OrganizationUnit(_tenantId, "First Floor", root.Id));
                        }
    
                        var secondFloor = _context.OrganizationUnits.IgnoreQueryFilters().FirstOrDefault(m => m.TenantId == _tenantId && m.DisplayName == "Second Floor");
                        if (secondFloor == null)
                        {
                            organizationUnitManager.Create(new OrganizationUnit(_tenantId, "Second Floor", root.Id));
                        }
    
                    }
                    _context.SaveChanges();
                }
            }
        }
    }
    
    

    Hope that helps.

  • User Avatar
    0
    -bitman created

    Thank you so much @bobingham

  • User Avatar
    0
    BobIngham created

    No problem, nice to take the weight from weight from the guys at Volosoft so they can get 8.2 out for @weedkiller three weeks ago and include sixteen extra items of functionality he wants....

  • User Avatar
    0
    -bitman created

    That is awesome. I really love this framework. Volosoft has made an amazing product. Thank you for assisting as well. I hope to be better with this framework so I can return back the favor some day.

  • User Avatar
    0
    BobIngham created

    It's no problem, if you're like me it will help you learn best practise and I'm always happy to help when I've done something before. But most of the stuff in the forum is like magic to me. I just leave it to the wizards and learn like an apprentice. Good luck! ****Note to ismcagdas, can I have a discount on my next renew?

  • User Avatar
    0
    -bitman created

    You and me both =)

  • User Avatar
    0
    murat.yuceer created

    Hi @bobingham did you try is your sulution work with separated tenant database?

  • User Avatar
    0
    BobIngham created

    Nope, I don't use separate tenant databases. Why, is this method a problem for separate databases?

  • User Avatar
    0
    murat.yuceer created

    Because you dont change connection when seed data

  • User Avatar
    1
    BobIngham created

    It wouldn't be a problem for me, I seed my tenants with a store procedure, that way the5re's no requirement for a release when seed data changes.