'Add prefix to id of entity

I am wondering whether or not it is possible to add a prefix to a value (in this case the primary key, the id) in ASP.NET Core.

I am using an abstract class with three implementations and I want to give each implementation a different prefix for the id. The abstract class is a Post with the following properties:

public abstract class Post
{
    [Key] public long PostId { get; set; }

    public long FromProfileId { get; set; }
    
    public string Content { get; set; }
    
    public DateTime? CreatedAt { get; set; }
    
    public DateTime? UpdatedAt { get; set; }
}

One of the implementations is a ProfilePost, which has the following contents:

public class ProfilePost : Post
{
    public long ToProfileId { get; set; }
}

Let's say I want to prefix the PostId with the P for Profile, so the first ProfilePost created would have id P1, the second one P2 and so forth.

And I will eventually also make a CommentPost entity which would have the prefix C.

How can I achieve this in ASP.NET Core?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source