Base solution for your next web application
Open Closed

Custom Entity With ForeignKey to User #8002


User avatar
0
rickfrankel created

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)
  • User Avatar
    0
    maliming created
    Support Team

    Can you share the structure of the Order table in the database? Or the migration class of the Order table.

  • User Avatar
    0
    rickfrankel created

    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 KEY IX_Orders_OrderNumber (OrderNumber), KEY IX_Orders_CanceledById (CanceledById), KEY IX_Orders_CompletedById (CompletedById), KEY IX_Orders_TenantId (TenantId), CONSTRAINT FK_Orders_AbpTenants_TenantId FOREIGN KEY (TenantId) REFERENCES abptenants (Id) ON DELETE CASCADE, CONSTRAINT FK_Orders_AbpUsers_CanceledById FOREIGN KEY (CanceledById) REFERENCES abpusers (Id) ON DELETE RESTRICT, CONSTRAINT FK_Orders_AbpUsers_CompletedById FOREIGN KEY (CompletedById) REFERENCES abpusers (Id) ON DELETE RESTRICT, ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

    Cheers Rick

  • User Avatar
    0
    maliming created
    Support Team

    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.

  • User Avatar
    0
    dexmox created

    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 ?

  • User Avatar
    0
    maliming created
    Support Team

    @dexmox

    Instead of using automapper, you can call ToList directly on IQueryable to see the results.

  • User Avatar
    0
    dexmox created

    @maliming, thats another good idea for OP.

  • User Avatar
    0
    rickfrankel created

    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.