I have generic structure for my rest API that is built on .net core 3.0
Here is my code for generic entities.
public interface IFullAuditedEntity
{
object Id { get; set; }
}
public abstract class FullAuditedEntity<T> : IFullAuditedEntity where T : struct
{
public IFullAuditedEntity() { }
[Key]
public virtual T Id { get; set; }
object Entity.Id
{
get { return Id; }
set
{
Id = (T)value;
}
}
}
Now, this structure works fine for me if my entity has int primary key.
But If I have to use string as a primary key, it gives me generics error. see below entity.
[Table(name: "Status")]
public class Status : FullAuditedEntity<string>
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
[Required]
[StringLength(03)]
public override string Id { get; set; }
[StringLength(03)]
public string Type { get; set; }
}
See added snap for more information.
I tried to pass Nullable as some of other SO answers suggested but it didn't work for me, so any help would be really appreciated.