I'm developing an ASP.NET Core Razor Pages application in .NET 5.0 using EF Core 5 with Identity for authentication/authorisation and I'm attempting to do this with a 'Clean Architecture'.
The application implements 3 'types' of user: Supervisors, Owners and Staff. Each are separate entities and so are persisted in their own db tables. Each user type has further related tables not shown here.
ApplicationUser (the principle entity, stored in the AspNetUsers table) needs a one-to-zero-or-one relationship to each of these user 'types' (the dependent entities):
//
// Infrastructure
//
namespace MyApp.Identity
{
public class ApplicationUser : IdentityUser<Guid>
{
...
}
}
//
// Core
//
namespace MyApp.Domain.Entities
{
public class Supervisor
{
public int Id { get; set; } // PK
public string Department { get; set; }
public Guid UserId { get; set; } // FK
...
}
public class Owner
{
public int Id { get; set; } // PK
public string Company { get; set; }
public Guid UserId { get; set; } // FK
...
}
public class Staff
{
public int Id { get; set; } // PK
public string Phone { get; set; }
public string PostCode { get; set; }
public string Notes { get; set; }
public Guid UserId { get; set; } // FK
...
}
}
ApplicationUser is in the Identity project within the Infrastructure layer as it is implementation specific (as it inherits from IdentityUser).
To avoid going against the clean architecture principles, I shouldn't allow EF Core to automatically setup the relationships by convention because I believe that would require the addition of an ApplicationUser navigation property to the Staff entity, which in turn would require a reference to the infrastructure layer?
Hence, it appears I need to use Fluent API but how do I define these one-to-zero-or-one relationships without having the navigation properties please?