R – Using Fluent NHibernate Auto Mapping to map IDs of type object from base Entity class

fluent-nhibernatenhibernatenhibernate-mapping

In the project I'm working on now, we have base Entity class that looks like this:

public abstract class Entity<T> where T : Entity<T>
{
    public virtual object Id { get; protected set }    
    // Equals, GetHashCode overrides, etc...
}

Most classes inheriting from Entity should map Id to int column in SQL Server database, but at least one will need to map to long (bigint).

Is it possible to create FluentNH Auto Mapping convention to map those object Ids to int by default?
Then we could use another convention or IAutoMappingOverride to handle long Ids.

Thanks!

Best Answer

To answer my own question... It's possible.

You can define convention like this:

internal class PrimaryKeyConvention : IIdConvention
{
    public bool Accept(IIdentityPart id)
    {
        return true;
    }

    public void Apply(IIdentityPart id)
    {
        if (<ID should be long>)
            id.SetAttribute("type", "Int64");
        else
            id.SetAttribute("type", "Int32");
    }
}
Related Topic