2

The mapping of a property that's of an enumerated type and is part of a composite key seems to have changed from int to string, and there's no way of changing it back.

I've got this data class:

public class Table5
{
    public virtual int Value { get; set; }

    public virtual Level Level { get; set; }

    public virtual string Name { get; set; }

    // Equality operators omitted
}

public enum Level
{
    Hi,
    Lo
}

with this mapping:

public class Table5Map : ClassMap<Table5>
{
    public Table5Map()
    {
        Table("Table5");

        CompositeId()
            .KeyProperty(x => x.Value)
            .KeyProperty(x => x.Level);

        Map(x => x.Name);
    }
}

The "Level" column in the database is an integer.

This used to work, but with his version of Fluent it attempts to write the strings "Hi" and "Lo" to the Level column.

How do I force it to map to an integer?

Nick
  • 31
  • 2

2 Answers2

4

The simplest way is just set Type for the key property:

CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, m => m.Type(typeof(int)));

FNH maps always enums as strings by default. On the contrary enum inside composite-id was mapped to int by default in FNH1.0. Probably it was changed in order to fix such inconsistency in higher versions.

Jakub Linhart
  • 4,062
  • 1
  • 26
  • 42
2

Try this change to your mapping:

/// snip ///
 CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, c => c.ColumnName("Level").Type(typeof(Level)));
/// snip ///

This "trick" worked for me in having to deal with a legacy database that had many composite keys. If you need the equivalent "trick" for non-key properties, look at the accepted answer here. Good luck and may all your databases not always be "legacy"!

Community
  • 1
  • 1
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51