Base solution for your next web application
Open Closed

TenantId is updating as zero in IMustHaveTenant entities #12350


User avatar
0
it@aldanaqatar.me created

Hi,
My application is Multitenancy enabled and all my entities are inherited from IMustHaveTenant class.
While creating new entities, the Tenant ID is updated properly, but when updating the same entity, it saves the Tenant ID as 0.

My NetZero Package: .NetCore & MVC - v 14.0.0.0
Target Framework: .Net 9
EFCore 9

Please note that I am not using repositories in my project (Bcz some old entities can't be inherited from the Entity base class).

Sample code (DbContextProvider is injected to do the transactions):

This saves the Tenant ID correctly:
_context.GetDbContext().AssetMaster_Company.Add(entity);
_context.GetDbContext().SaveChanges();

And this below will not:
_context.GetDbContext().AssetMaster_Company.Attach(entity);
_context.GetDbContext().Entry(entity).State = EntityState.Modified;
_context.GetDbContext().SaveChanges();

I hope I don't need to specify the Tenant ID in every entity transaction.


9 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    EF Core does not understand or manage inherited properties (IMustHaveTenant.TenantId) directly unless:
    It is explicitly handled in the DbContext.
    Or, the TenantId is included in every update manually. As a result, during the update, TenantId is overwritten with 0.
    When updating the entity, are you manually assigning the information of the currently logged-in user from AbpSession?

  • User Avatar
    0
    it@aldanaqatar.me created

    Hi,
    I know the EFCore does not insert a value into the IMustHaveTenant property, but the ABP framework does. It inserts the logged-in user's details automatically without assigning them. We also expect the same behaviour for TenantId.

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    How do you get the entity in the section you update, can you detail the update code you gave as an example and share it with us?

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    When adding an entity, the TenantId is set. You can review the code here if you’d like. When updating, you can retrieve the entity from the database to access the TenantId before updating it. Alternatively, you can override this method here to set the TenantId.

  • User Avatar
    0
    it@aldanaqatar.me created

    Oh.. so, the auto-set option is now available only for newly inserting entities. Is there any chance of a performance issue if I override the ApplyAbpConceptsForModifiedEntity method to use CheckAndSetMustHaveTenantIdProperty?

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    Overwriting the ApplyAbpConceptsForModifiedEntity method and using CheckAndSetMustHaveTenantIdProperty may introduce some additional processing overhead, but it generally does not have a significant impact on performance. The CheckAndSetMustHaveTenantIdProperty method will only execute for entities that implement the IMustHaveTenant interface and will simply set the TenantId value with a basic if check.

  • User Avatar
    0
    it@aldanaqatar.me created

    May I ask why you excluded the process when modifying the entity? If the entity inherits to IMustHaveTenant, we must ensure it when updating it, too, right?

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    Since the entity will already come from the database with its TenantId value during the update in the default Abp repository and AbpDbContext structure, there is no need to set the TenantId in the relevant method during the entity update.

  • User Avatar
    0
    it@aldanaqatar.me created

    Okay.. Thanks for your response.