Base solution for your next web application
Open Closed

How to extend OU unit entity with some property ? #1330


User avatar
0
klainer created

Hello, I need to extend OU unit entity with some property, is this even possible ? If so, What is proper way to do that ? I need to store aditional data about entity (another code), only for manage users..

Thanks for tips!


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

    Hi,

    Please see this forum topic #785@6f2631bf-217f-4a12-82d4-9601d72aca06

  • User Avatar
    0
    klainer created

    Thanks. Works!

    I have another question. In CORE project i create MYOrganizationUnitManager class. I need to insert orgnization unit there:

    public class MYOrganizationUnitManager : IDomainService
        {
            private readonly UserManager _userManager;
            private readonly IRepository<OrganizationUnit, long> _organizationUnitRepository;
            private readonly OrganizationUnitManager _organizationUnitManager;
            public MYOrganizationUnitManager(
                UserManager userManager,
                IRepository<OrganizationUnit, long> organizationUnitRepository,
                OrganizationUnitManager organizationUnitManager
                )
            {
                this._userManager = userManager;
                _organizationUnitRepository = organizationUnitRepository;
                _organizationUnitManager = organizationUnitManager;
            }
    
    
            public virtual async Task CreateOuStructure()
            {
                // Remove all OU first
    
                // Get ROOT OU
                var rootUnit = _organizationUnitRepository.GetAll().FirstOrDefault(x => x.DisplayName == "ROOT");
                if (rootUnit == null)
                {
                    // Create fresh root unit
                    rootUnit = await CreateOu("ROOT", "EN000000");
    
                }
    
                  var regionUnit = await CreateOu("Child UNIT", "EN570000", rootUnit.Id);
           }
    
    
    
            protected virtual async Task<MYOrganizationUnit> CreateOu(string displayName, string hermesUnitCode, long? parentId = null)
            {
                var ou = new MYOrganizationUnit() { DisplayName = displayName, CustomUnitCode = customUnitCode ,ParentId = parentId };
                await _organizationUnitManager.CreateAsync(ou);
                await _organizationUnitManager.UnitOfWorkManager.Current.SaveChangesAsync();
    
                return ou;
            }
    }
    

    I´m using this (MYOrganizationUnitManager ) in MYApplicationService, and I got this error:

    Abp.AbpException: There is no such an entity with given primary key. Entity type: Abp.Organizations.OrganizationUnit, primary key: 13 
    v Abp.Domain.Repositories.AbpRepositoryBase`2.&lt;GetAsync&gt;d__16.MoveNext() v D:\Halil\GitHub\aspnetboilerplate\src\Abp\Domain\Repositories\AbpRepositoryBase.cs:řádek 81 
    --- Konec trasování zásobníku z předchozího místa, ze kterého byla vyvolána výjimka --- 
    v System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    v System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    v System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    v Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext() v D:\Halil\GitHub\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:řádek 120 
    --- Konec trasování zásobníku z předchozího místa, ze kterého byla vyvolána výjimka --- 
    v System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    v System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    v System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    v Abp.Organizations.OrganizationUnitManager.<GetCodeAsync>d__10.MoveNext() v D:\Halil\GitHub\module-zero\src\Abp.Zero\Organizations\OrganizationUnitManager.cs:řádek 67 
    --- Konec trasování zásobníku z předchozího místa, ze kterého byla vyvolána výjimka --- 
    v System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    v System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    

    This code do not insert new entity to DB, but ID is generated, when iserting new entity i get above error

    await _organizationUnitManager.UnitOfWorkManager.Current.SaveChangesAsync();
    

    ¨

    Thanks for help

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Instead of this line

    await _organizationUnitManager.UnitOfWorkManager.Current.SaveChangesAsync();
    

    just inject IUnitOfWorkManager in your domain service, start a new unitOfWork and use SaveChangesAsync method of it. <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">http://aspnetboilerplate.com/Pages/Docu ... it-Of-Work</a>