2

I've just started using FluentMap and I'm looking to map the same column to 2 properties, as they need the same value. Here's my map:

internal class DefaultsMap : EntityMap<Defaults>
{
    internal DefaultsMap()
    {
        Map(d => d.HistoricalValues.Cost).ToColumn("defaultValue");
        Map(d => d.FutureValues.Cost).ToColumn("defaultValue");
        //other mappings...
    }
}

This throws the following error:

Exception: Duplicate mapping detected. Property 'Cost' is already mapped to column 'Cost'.

So it looks like I can't map the same column to 2 different properties, or is this because the properties themselves are called the same thing (Cost)?

sr28
  • 4,728
  • 5
  • 36
  • 67

2 Answers2

1

Not with dapper; I had this problem with NHibernate.

I am not sure about this but may be mapper tool stores your mappings in some kind if key-value like data structure.

That said, you cannot store two different keys with same name.

Even if this is not true, it is obvious that mapper must need distinct naming to identify that the map is different. It does not look at the class for which the property is defined; it just looks at the property name.

You may consider renaming your properties to name them explicitly like below:

HistoricalValues.Cost => HistoricalValues.HistoricalCost
FutureValues.Cost => FutureValues.FutureCost
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Yes, unfortunately both HistoricalValues and FutureValues are of the same type and both are filled with defaults from a defaults table that's a generic store for default values. I have found a bit of hack / workaround by simply doing HistoricalValues.Cost for one and FutureValues.Cost.Value for the other. That seems to work. – sr28 Jun 14 '19 at 08:27
0

As per Amit Joshi's answer it doesn't look like this is possible in it's current form as the mapper must have distinct naming. Unfortunately, I'm not able to rename the column for just one of them as the query is returning all values from a generic table to create a list of Defaults objects.

As a workaround, in this case I've been able to achieve what I wanted by assigning to the property for one of them and to the 'Value' property of the 'Cost' property for the other like this:

Map(d => d.HistoricalValues.Cost).ToColumn("defaultValue");
Map(d => d.FutureValues.Cost.Value).ToColumn("defaultValue");
sr28
  • 4,728
  • 5
  • 36
  • 67