Base solution for your next web application
Open Closed

Link tables and repositories #7250


User avatar
0
BobIngham created

When I have a link table like the NcDocumentDocumentTag table below: How do I return the relevant NcDocumentTag records for the NcDocument table? In Entity Framework the NcDocumentTag table would normally be loaded through lazy loading but using Abp's repositories as follows doesn't work:

            var NcDocument = await _ncDocumentRepository.GetAll()
                .Where(m => m.Id == input.Id)
                .Include(m => m.DocumentTags)
                .FirstOrDefaultAsync();

And I would not have to create an entity in my Core project for the link table but that doesn't seem to work with the .Include() extension. Any ideas anyone?


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Can you share the code for these two entities?

  • User Avatar
    0
    BobIngham created

    Hi @maliming, Thanks for getting back.

        [Table("NcDocument")]
        public class NcDocument : FullAuditedEntity<int>, IMustHaveTenant, IExtendableObject
        {
            public int TenantId { get; set; }
            public int NcDocumentTypeId { get; set; }
            public int? NcDocumentTemplateId { get; set; }
            public int NcDocumentCategoryId { get; set; }
            public string DisplayName { get; set; }
            public string ReportString { get; set; }
            public string ExtensionData { get; set; }
            public string Uri { get; set; }
            public virtual NcDocumentTemplate DocumentTemplate { get; set; }
            public virtual NcDocumentCategory DocumentCategory { get; set; }
            public virtual NcDocumentType DocumentType { get; set; }
            public List<NcDocumentTag> DocumentTags { get; set; }
            public List<NcDocumentHistory> DocumentHistory { get; set; }
        }
        
        [Table("NcDocumentTag")]
        public class NcDocumentTag : IMustHaveTenant
        {
            public int Id { get; set; }
            public int TenantId { get; set; }
            public int NcDocumentTagTypeId { get; set; }
            public string DisplayName { get; set; }
        }
    
  • User Avatar
    0
    maliming created
    Support Team

    NcDocumentTag and NcDocument are many-to-many relationships from the definition of the database table, but from the definition of your entity is a one-to-many relationship.

    Description of the entity relationship: https://www.entityframeworktutorial.net/entity-relationships.aspx

  • User Avatar
    0
    BobIngham created

    @maliming, That's great, I understand relationships but they key here is "Open EDM in XML" and "put into the NcDocumentDocumentTag table in the <AssociationSetMapping/> section.". How, exactly, do I do that with .Net Zero. This isn't a show stopper, I will refactor and place an Id column in my link table and use .ThenInclude() to get around the problem. But I was wondering....

  • User Avatar
    0
    maliming created
    Support Team

    What is your zero version? Ef or er core?

    Zero is always the EF code first mode.

  • User Avatar
    0
    BobIngham created

    .NET Core, Angular, 6.8.0, aspnet framework 4.6. I have refactored and continued but would still like to know best practise.