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.

Extend ApplicationUser with Domain entities relationship

See original GitHub issue

Hello,

I was wondering how one could extend the ApplicationUser the cleanest way possible if I need a one-to-many relationship between ApplicationUser and one entity from my Domain project. Should I just move ApplicationUser to my Domain model? Right now, I cannot do it since my domain model has no idea about ApplicationUser.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
GFoley83commented, Feb 20, 2020

Then you already have multiple options listed above to accomplish that.

The easiest and quickest way is to simply have Entity inherit from AuditableEntity. This way the CreatedBy property will be populated with the current user’s ID automatically when Entity is added to the Db.

When retrieving Entity from the Db, just inject ICurrentUserService into your Get Entity handler and select where CreatedBy == _currentUserService.UserId.

E.e.

public class GetEntityQueryHandler : IRequestHandler<GetEntityQuery, EntityDto>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;
    private readonly ICurrentUserService _currentUserService;

    public GetEntityQueryHandler(IApplicationDbContext context,
        ICurrentUserService currentUserService)
    {
        _context = context;
        _currentUserService = currentUserService;
    }

    public async Task<EntityDto> Handle(GetEntityQuery request, CancellationToken cancellationToken)
    {
        var entity = _context.Entity
            .FirstOrDefaultAsync(i => i.CreatedBy == _currentUserService.UserId);

        return _mapper.Map<Entity, EntityDto>(entity);
    }
}

In any case, your issue is a software design issue and not an issue related to this project. The above comments more than cover the issue you described. If you need further help, I suggest you post a question on StackOverflow (where questions like this belong).

Good luck.

1reaction
jasontaylordevcommented, Mar 31, 2020

Thanks for your help @GFoley83. 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add relationship to .NET Identity ApplicationUser for entity ...
I dont know how to be able to place the AppUser class in my data layer so that IdentityConfig can be able to...
Read more >
Extending IdentityUser With Custom Properties in ASP.NET
In this article, we'll learn all possibilities of extending IdentityUser with custom properties and changing the primary key type.
Read more >
Part 2: Creating the Domain Models
This tutorial shows how to create domain models by using the code-first approach to Entity Framework.
Read more >
Identity model customization in ASP.NET Core
This article describes how to customize the underlying Entity Framework Core data model for ASP.NET Core Identity.
Read more >
Domain-Driven Design in ASP.NET Core applications
A library of basic concepts of Domain-Driven Design pattern and provide a practical example of how to apply DDD in a typical ....
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