Jeff B
Jeff B

Reputation: 337

How To Write Automapping Convention That Uses Sequence

I'm with updating some of our mappings from the fluent mapping paradigm to the auto mapping paradigm. I have a class named Group where the current fluent mapping for the identity column looks like this:

Id(x => x.Id, "ID")
    .GeneratedBy
    .Native("GROUPS_SEQ");

Resulting in HBM that looks like this:

<id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="ID" />
  <generator class="native">
    <param name="sequence">GROUPS_SEQ</param>
  </generator>
</id>

This works great with our Oracle database AND with tests that use SQLite. Unfortunately I can't figure out how to create an IIdConvention convention that will give me the same result. I've gotten this far:

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {
        string pluralized = Inflector.Net.Inflector.Pluralize(instance.EntityType.Name);
        string underscored = Inflector.Net.Inflector.Underscore(pluralized);
        string uppercased = underscored.ToUpper();

        string sequenceName = string.Format("{0}_SEQ", uppercased);

        instance.Column("Id");
        instance.UnsavedValue("0");
        instance.GeneratedBy.Native(sequenceName);
    }
}

Unfortunately the last line causes a compile error because the .Native method doesn't accept a sequence name string like it does in the fluent mappings. Does anyone have any suggestions on how best to solve this?

Thanks!

Upvotes: 1

Views: 698

Answers (1)

Jeff B
Jeff B

Reputation: 337

The GeneratorInstance.cs file already contains implementations for the following methods:

void Native(string sequenceName);
void Native(string sequenceName, Action<ParamBuilder> paramValues);

The methods are not exposed in the IGeneratorInstance.cs interface. I went ahead and added them which allowed me to create the primary key convention that I need. As near as I can tell they work fine. Unfortunately my company's IT security group has seen fit to close a bunch of ports on their firewall which prevents me from contributing this back via github. So if anyone wants to submit the update for me I would appreciate it.

Thanks!

Upvotes: 1

Related Questions