我有一个IDictionary<StocksLocation,decimal>属性的映射,这是一个映射:
HasMany<StocksLocation>(mq => mq.StocksLocation)
.KeyColumn("IDProduct")
.AsEntityMap("IDLocation")
.Element("Quantity", qt => qt.Type<decimal>()); 现在,我从decimal更改为值对象:Quantity。
Quantity有两个属性,十进制Value和Unit单位(其中Unit是枚举)。
我现在必须映射IDictionary<StocksLocation,Quantity>,我如何实现这一点?
提前感谢
发布于 2010-03-18 11:38:00
选项1:将其映射为实体
我猜你的表看起来像这样:
CREATE TABLE Quantity (
ID int NOT NULL,
IDProduct int NOT NULL,
IDLocation int NOT NULL,
Value decimal(18,2) NOT NULL,
Unit int NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (IDProduct) REFERENCES Product (ID),
FOREIGN KEY (IDLocation) REFERENCES StocksLocation (ID),
UNIQUE KEY (IDProduct, IDLocation)
);继续并将Quantity映射为实体类:
public class QuantityMap : ClassMap<Quantity>
{
public QuantityMap()
{
Id(x => x.Id);
References(x => x.Product, "IDProduct");
References(x => x.Location, "IDLocation");
Map(x => x.Value);
Map(x => x.Unit);
}
}..。然后将Product.StocksLocation映射更改为:
HasMany<StocksLocation, Quantity>(mq => mq.StocksLocation)
.KeyColumn("IDProduct")
.AsMap(x => x.Location); 选项2:将其映射为组件
因为您说过不希望将Quantity映射为实体,所以让我们考虑如何将其映射为组件。Product.StocksLocation字典的*.hbm.xml映射如下所示:
<map name="StocksLocation" table="Quantity">
<key column="IDProduct" />
<index-many-to-many column="IDLocation" class="YourNamespace.StocksLocation, YourAssembly" />
<composite-element class="YourNamespace.Quantity, YourAssembly">
<property name="Unit" type="YourNamespace.Unit, YourAssembly" />
<property name="Value" type="System.Decimal, mscorlib" />
</composite-element>
</map>我们如何用FluentNHibernate做到这一点呢?据我所知,在主干中没有这样做的方法,所以你有几个选择:
HasManyComponent方法。他有一个到他的项目源代码的链接,但我不知道那个源代码是否包括他对FluentNHibernate.ClassMap类,并为无法使用FluentNHibernate.映射的任何类编写*.hbm.xml文件
https://stackoverflow.com/questions/2424180
复制相似问题