Search code examples
c#linqservicestackormlite-servicestack

Adding ServiceStack OrmLite attributes in code instead of a property


Does ServiceStack.OrmLite support attributes using Linq like in EntityFramework?

Instead of decorating every property with [PrimaryKey] or [CustomField], have a initializer class that uses LinQ to set-up attributes for every property.

Something like

Entity<User>().SetCustomField(p => p.Id, Entity.PrimaryKey);

Possible?


Solution

  • In OrmLite Id is automatically the Primary Key, otherwise the first property is assumed to be the primary key. But you can also use ServiceStack's dynamic attribute API to add attributes dynamically on StartUp, e.g:

    typeof(User)
        .GetProperty("Id")
        .AddAttributes(new PrimaryKeyAttribute());