Hi,
I have a custom entity that has multiple foreignkeys to user. Even after including them in the query they don't populate.
EG:
If I have an Order entity defined as (simplified for example)
[Table("Orders")] [Audited] public class Order : FullAuditedEntity, IMustHaveTenant {
public int TenantId { get; set; }
[ForeignKey("TenantId")]
public virtual Tenant TenantFk { get; set; }
public virtual bool Completed { get; set; }
public virtual long? CompletedById { get; set; }
[ForeignKey("CompletedById")]
public virtual User CompletedByFk { get; set; }
public virtual bool Canceled { get; set; }
public virtual long? CanceledById { get; set; }
[ForeignKey("CanceledById")]
public virtual User CanceledByFk { get; set; }
}
When I load up the order using an IRepository<Order> and Include(_ => _.CompletedByFk) for example. It never populates the CompletedByFk object, even though the CompletedById has a value and it maps to a valid row in that AbpUsers table.
Any thoughts?
Thanks
Rick
7 Answer(s)
-
0
Can you share the structure of the Order table in the database? Or the migration class of the Order table.
-
0
Note that the full table below isn't represented but the key parts are I believe.
It has the constraint in place to the abpusers table. (Note I'm using MySql). All my other foreign key constraints work perfectly. Tenant has no issue. Just Users.
This is the only place I've had another foreign key to Users before like this. Other than the ones provided by the Auditing interfaces.
CREATE TABLE
orders
(Id
int(11) NOT NULL AUTO_INCREMENT,CreationTime
datetime(6) NOT NULL,CreatorUserId
bigint(20) DEFAULT NULL,LastModificationTime
datetime(6) DEFAULT NULL,LastModifierUserId
bigint(20) DEFAULT NULL,IsDeleted
bit(1) NOT NULL,DeleterUserId
bigint(20) DEFAULT NULL,DeletionTime
datetime(6) DEFAULT NULL,TenantId
int(11) NOT NULL,OrderNumber
varchar(255) DEFAULT NULL,OrderDate
datetime(6) NOT NULL,Completed
bit(1) NOT NULL,CompletedById
bigint(20) DEFAULT NULL,Canceled
bit(1) NOT NULL,CanceledById
bigint(20) DEFAULT NULL, PRIMARY KEY (Id
), UNIQUE KEYIX_Orders_OrderNumber
(OrderNumber
), KEYIX_Orders_CanceledById
(CanceledById
), KEYIX_Orders_CompletedById
(CompletedById
), KEYIX_Orders_TenantId
(TenantId
), CONSTRAINTFK_Orders_AbpTenants_TenantId
FOREIGN KEY (TenantId
) REFERENCESabptenants
(Id
) ON DELETE CASCADE, CONSTRAINTFK_Orders_AbpUsers_CanceledById
FOREIGN KEY (CanceledById
) REFERENCESabpusers
(Id
) ON DELETE RESTRICT, CONSTRAINTFK_Orders_AbpUsers_CompletedById
FOREIGN KEY (CompletedById
) REFERENCESabpusers
(Id
) ON DELETE RESTRICT, ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;Cheers Rick
-
0
HI @rickfrankel
There is no problem with your Order Table.
Your query code might be:
GetAll().Include(x => x.CompletedByFk ).Include(x => x.CanceledByFk )
Also please note: The TenantId of the user corresponding to Order's CompletedById (CanceledById) should be the same as the TenantId of the Order.
-
0
Could this maybe have something to do with automapper being unable to match the include ?
For example the mapping dto property name does not match the field name CanceledByFk ?
-
0
@dexmox
Instead of using automapper, you can call ToList directly on IQueryable to see the results.
-
0
@maliming, thats another good idea for OP.
-
0
Ok problem solved and it was me being an idiot :)
You have to do this to ensure it disables the filters for AbpUsers using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MustHaveTenant, AbpDataFilters.MayHaveTenant))
And NOT just using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MustHaveTenant))
Because the host user has no tenant ID the tenantID in the AbpUsers table is nullable and thus only MayHaveTenant. It was working for all my other foreign keys as they were all MustHaveTenant.
Thanks all.