C
C#6mo ago
joren

Using base class as relationship definition in model ASP.net WEB API

So I've written this model that I used to save my credentials to authenticate my users with:
public class UserCredentialsModel : IdentityUser, IModelEntity
{
[Required]
string Id { get; set; }

[Required]
public DateTime CreatedAt { get; set; }

public ICollection<RefreshTokenModel> RefreshTokens { get; } = new List<RefreshTokenModel>();

// Does this work?
public BaseProfile profile { get; set; }
}
public class UserCredentialsModel : IdentityUser, IModelEntity
{
[Required]
string Id { get; set; }

[Required]
public DateTime CreatedAt { get; set; }

public ICollection<RefreshTokenModel> RefreshTokens { get; } = new List<RefreshTokenModel>();

// Does this work?
public BaseProfile profile { get; set; }
}
Now as you can see I have a BaseProfile here, each credential has its connected profile attached. It is a one to one relationship. However, I have multiple types of profiles, that have a few things in common so I wrote a base class like this:
public class BaseProfile : IModelEntity
{
public string Id { get; set; }


[Required]
public string FirstName { get; set; }

[Required]
public string LastName { get; set; }

public UserCredentialsModel UserCredentials { get; set; }
}
public class BaseProfile : IModelEntity
{
public string Id { get; set; }


[Required]
public string FirstName { get; set; }

[Required]
public string LastName { get; set; }

public UserCredentialsModel UserCredentials { get; set; }
}
and then I have different profiles that inherit from this, would this relationship still work as intended when using Baseprofile? Or do I need to have a field for each profile and have them as nullable/have some enum to decide the profile type and use that as a way to differentiate
1 Reply
joren
joren6mo ago
I would assume I'd need all my profiles inside UserCredentialsModel, make them nullable and define the relation in OnModelCreate accordingly?
public class UserCredentialsModel : IdentityUser, IModelEntity
{
[Required]
string Id { get; set; }

[Required]
public DateTime CreatedAt { get; set; }

public ICollection<RefreshTokenModel> RefreshTokens { get; } = new List<RefreshTokenModel>();

public CompanyUserModel? CompanyProfile { get; set; }
public PanelUserModel? PanelProfile { get; set; }
public PortalManagerUserModel? PortalManagerProfile { get; set; }

}
public class UserCredentialsModel : IdentityUser, IModelEntity
{
[Required]
string Id { get; set; }

[Required]
public DateTime CreatedAt { get; set; }

public ICollection<RefreshTokenModel> RefreshTokens { get; } = new List<RefreshTokenModel>();

public CompanyUserModel? CompanyProfile { get; set; }
public PanelUserModel? PanelProfile { get; set; }
public PortalManagerUserModel? PortalManagerProfile { get; set; }

}
is what I did now, is this a decent solution or am I looking at it the wrong way?