Automapping control of values in discriminator column

MylesRip's Avatar

MylesRip

18 May, 2010 03:25 PM via web

When using Automapping, the full class name, including the namespace, is used as the default value in the discriminator column. I know this can be overridden individually with either fluent mappings or hbm mappings, but I'd like to know if there is a way to configure the Automapper to generate these values differently. I'd like it to use the unqualified class name (without the namespace). Is there currently a way to do this?

  1. Support Staff 2 Posted by James Gregory on 19 May, 2010 02:04 AM

    James Gregory's Avatar

    That should be easy. If you haven't already, have a read up on our conventions; specifically, have a read of writing your own conventions.

    What you need in this case is an ISubclassConvention, which will allow you to control the discriminator value for subclasses.

    Here's the convention you'll need:

    public class DiscriminatorConvention : ISubclassConvention
    {
      public void Apply(ISubclassInstance instance)
      {
        instance.DiscriminatorValue(instance.Type.Name);
      }
    }
    

    Here's how you hook it up to your automapping:

    AutoMap.AssemblyOf<Person>(new YourConfiguration())
      .Conventions.Add<DiscriminatorConvention>();
    

    There are other methods available on the Conventions property, if you've got more than one you need to add.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is five times five?