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)
-
0
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? -
0
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. -
0
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?
-
0
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.
-
0
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?
-
0
Hi
Overwriting the
ApplyAbpConceptsForModifiedEntity
method and usingCheckAndSetMustHaveTenantIdProperty
may introduce some additional processing overhead, but it generally does not have a significant impact on performance. TheCheckAndSetMustHaveTenantIdProperty
method will only execute for entities that implement theIMustHaveTenant
interface and will simply set theTenantId
value with a basicif
check. -
0
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?
-
0
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.
-
0
Okay.. Thanks for your response.