Fluent NHibernate - Matching a dictionary of component type / value objects as HasMany

I have an Item class that has many Rates . They are associated with the listing, RateType .

 public class Item { int Id {get;set;} IDictionary<RateType, Rate> Rates {get;set;} // some other stuff } public class Rate { RateType Type {get;set;} decimal Amount {get;set;} decimal Quantity {get;set;} } 

I redefine my mapping like this:

 public void Override(FluentNHibernate.Automapping.AutoMapping<Item> mapping) { mapping.HasMany(x => x.Rates) .AsMap(x => x.Type) .KeyColumns.Add("Item_Id") .Table("InvoiceItem_Rates") .Component(x => x.Map(r => r.Amount)) .Component(x => x.Map(r => r.Quantity)) .Cascade.AllDeleteOrphan() .Access.Property(); } 

This has two problems.

1) When I take an element, Type is placed as the key of the Dictionary without problems. However, it is not assigned to the Type property within Rate .

2) I expect three columns in the InvoiceItem_Rates table ( Item_Id , Type , Quantity and Amount . However, Amount suspiciously missing.

Why is all this happening? What am I doing wrong?

+4
source share
1 answer

This is not ideal, in my opinion, since the value of the enum key is actually stored as an integer, not a string, but this is probably not a problem. The key point here is that you cannot have multiple calls for Component, as it will overwrite your previous Component call with any last one. The correct way to call Component () is as follows:

  Id(x => x.Id); HasMany(x => x.Rates) .AsMap(x => x.Type) .KeyColumn("Item_Id") .Table("InvoiceItem_Rates") .Component(x => { x.Map(r => r.Amount); x.Map(r => r.Quantity); }) .Cascade.AllDeleteOrphan(); 
+3
source

Source: https://habr.com/ru/post/1312242/


All Articles