开发者

HasMany relation inside a Join Mapping

So, I'm having a problem mapping in fluent nhibernate. I want to use a join mapping to flatten an intermediate table: Here's my structure:

[Vehicle]
VehicleId
...

[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...

[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue

Note that DTValueRange does not have a VehicleID. I want to flatten DTVehicleValueRange开发者_开发百科 into my Vehicle class. Tgis works fine for AverageValue, since it's just a plain value, but I can't seem to get a ValueRange collection to map correctly.

    public VehicleMap()
    {
        Id(x => x.Id, "VehicleId");
        Join("DTVehicleValueRange", x =>
        {
            x.Optional();
            x.KeyColumn("VehicleId");
            x.Map(y => y.AverageValue).ReadOnly();
            x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
        });
    }

The HasMany mapping doesn't seem to do anything if it's inside the Join. If it's outside the Join and I specify the table, it maps, but nhibernate tries to use the VehicleID, not the DTVehicleValueRangeId.

What am I doing wrong?


Can you explain the average value column in the DTVehicleValueRange table? Isn't this a calculated value (i.e. no need to persist it)?

It looks like you have a many-to-many relationship between Vehicle and DTValueRange, which of course would not be mapped with a join, rather with a HasManyToMany call.


Ran into a similar issue today using a Map to create a view. The SQL generated showed it trying to do the HasMany<> inside the join based on the Id of the ParentThing and not WorkThing (same problem you were having)

After much mapping of head-to-desk it turns out adding the propertyref onto the hasmany solved it.

public class ThingMap : ClassMap<WorkThingView> { 
     public ThingMap() {
                ReadOnly();
                Table("ParentThing");
                Id(x => x.ParentThingId);
                Map(x => x.ParentName);
                Join("WorkThing", join => {
                    join.KeyColumn("ParentThingId");
                    join.Map(m => m.FooCode);
                    join.Map(m => m.BarCode);
                    join.Map(x => x.WorkThingId);
                    join.HasMany(x => x.WorkThingCodes)
                        .Table("WorkThingCode").KeyColumn("WorkThingId").PropertyRef("WorkThingId")
                        .Element("WorkThingCode");
                });
            }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜