question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapper entities without foreign key

See original GitHub issue

@acjh Good morning, I am working with the AuditLog entity. I want to implement a service that returns all the logs that have been generated, and that these come with the user who performed the action. When I use the repository with the GetAllInclude method I cannot link the user because it is not the entity’s foreign key. How can I return the mapped object with user information? I want to replace GetAll by GetAllInclude. My code

Service:

public async Task<PagedResultDto<AuditLogDto>> GetAllAudityLog(PagedAuditLogResultRequestDto input)
        {
           var query = _auditLogRepository.GetAll()
                .WhereIf(input.UserId > 0, t => t.UserId == input.UserId)
                .Take(input.MaxResultCount)
                .Skip(input.SkipCount);
            var auditLogs = await query.ToListAsync();

            return new PagedResultDto<AuditLogDto>
            {
                TotalCount = auditLogs.Count,
                Items = ObjectMapper.Map<List<AuditLogDto>>(auditLogs)
            };
        }

DTO:

[AutoMapFrom(typeof(AuditLog))]
   public class AuditLogDto : EntityDto<long>
   {
       public DateTime ExecutionTime { get; set; }
       
       public string Exception { get; set; }
       
       public string BrowserInfo { get; set; }
       
       public string ClientName { get; set; }
       
       public string ClientIpAddress { get; set; }
       
       public int ExecutionDuration { get; set; }
       
       public string ReturnValue { get; set; }
       
       public string MethodName { get; set; }
       
       public string ServiceName { get; set; }
       
       public long? UserId { get; set; }
       
      public string Parameters { get; set; }
      
      public string CustomData { get; set; }
      
      // How fill this field in mapper
      public UserDto User { get; set; }
   }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
demirmusacommented, Jan 27, 2020

You can check Queryable.Join (System.Linq) as @acjh mentioned. There are similar examples in there.

2reactions
demirmusacommented, Jan 27, 2020
from auditLog in _auditLogRepository.GetAll()
join user in _userRepository.GetAll() on auditLog.UserId equals user.Id into userJoin
from joinedUser in userJoin.DefaultIfEmpty()
where auditLog.UserId == input.UserId 
select new AuditLogAndUser { AuditLog = auditLog, User = joinedUser };
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make Many-to-one mapping without foreign key in ...
1 Answer. Keyless entities (entity without key) cannot be principal of a relationship, because there is no key to be referenced by the...
Read more >
Relationships, navigation properties, and foreign keys - EF6
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data...
Read more >
Map one object to another object without using foreign key
hi, is there any solution to map one object into another object without using foreign key? for example, something like bellow: @Entity ......
Read more >
What's the deal with mapping foreign keys using the Entity ...
Mapping foreign keys is not a good idea from a purist's point of view because it results in storage concerns leaking into the...
Read more >
Designing for Related Data without Foreign Keys | Blog
Foreign Keys without the constraints. You don't have to configure a foreign key constraint on a column just because it refers to another...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found