OrmLite ExcludePropertyNames

mythz had a great writeup in this StackOverflow post about OrmLite using JsConfig.ExcludePropertyNames to control field mapping:

I am not seeing this working, and I don’t see it mentioned in any sample or docs. I’m using 5.8.0 on Core 3.1, I put the JsConfig<Terminal>.ExcludePropertyNames = new[] {"Prop1Fk", "Prop2Fk"}; in the AppHost.Configure() method. The breakpoint hits it, but when I then do:

var t = Db.Single<Terminal>(x => x.Id == 1);

I get errors telling me that Prop1Fk is not a valid column.

Any pointers on where I went wrong? Thanks.

JsConfig is for configuring the serializers which only impacts serializatiion, you can use the [Ignore] attribute to ignore properties in OrmLite.

Ah. That makes sense, given the JsConfig prefix there…

Is there any other mechanism for decoupling the mapping from the class? The only other thing I can think of is to create a new class for the DTO instead of trying to reuse an existing EF model. Putting the [Ignore] attribute on the EF class doesn’t sound great, but duplicating code isn’t great either.

IMO it’s not wise to reuse EF’s existing data models in OrmLite, esp. if you don’t want to use OrmLite attributes to control it’s behavior in OrmLite. You can always share a base class then have EF Properties in EF base class and OrmLite properties in OrmLite’s data model. If you do have 2 separate classes you can use the built-in AutoMapping to map the properties when you need to.

The other approach is dynamically adding attributes, this needs to be configured once on Startup before classes are accessed (e.g. in AppHost constructor) and the attributes are only visible to ServiceStack libraries, so you could add the [Ignore] attribute dynamically like:

public class AppHost : AppHostBase 
{
    public AppHost() {
        typeof(MyPoco)
            .GetProperty(nameof(MyPoco.EfProperty))
                .AddAttributes(new IgnoreAttribute());
    }
}
1 Like