Search code examples
c#servicestackdata-annotationsormlite-servicestack

How to retrieve Data Annotation Alias("tablename") and Alias("field name") from ServiceStack ORMLite?


I need to return the alias from the Data Annotation of this class "tblAccounts" and "AccountNumber". It uses ServiceStack ORM Lite.

[Alias("tblAccounts")]
    [Schema("task")]
    public class Account : IHasId<int>
    {
        [Alias("AccountNumber")]
        public int Id { get; set; }
        [Required]
        public int UnitId { get; set; }
        [Required]
        public int OldAccountNumber { get; set; }
        [Required]
    }

Solution

  • You can query this from OrmLite's ModelDefinition that's created for every POCO Table, e.g:

    var modelDef = typeof(Account).GetModelMetadata();
    
    var tableName = modelDef.ModelName;
    var idName = modelDef.PrimaryKey.FieldName;
    

    Which in both cases will return the [Alias] if it exists.