我有文章和图片。
图像是价值对象,文章是实体。图像被映射为
之类的组件public class ImageMap
{
public static Action<IComponentMapper<Image>> Mapping()
{
return c =>
{
c.Property(p => p.AltText);
c.Property(p => p.Name, m =>
{
m.Length(255);
});
c.Property(p => p.Path, m =>
{
m.Length(255);
});
c.Property(p => p.Height);
c.Property(p => p.Width);
c.Parent(x => x.Article, p => p.Access(Accessor.ReadOnly));
};
}
}
我不知道如何通过代码方法映射nhibernate映射中的组件列表 在其他组件中,只有一个对象而不是集合,我会使用这个
ArticleMap
Component(c => c.Address, AddressMap.Mapping());
如何映射组件(图像)的集合?
Article.cs
public virtual IList<Image> Images {get; set;}
答案 0 :(得分:2)
我们需要的是7.2. Collections of dependent objects。虽然原则与5.1.13. <component>
的情况几乎相同,但在这种情况下,元素名称为<composite-element>
。 实际上,对于我们需要IComponentElementMapper<>
所以,这将是我们的映射方法
public class ImageMap
{
// the <composite-element> as IComponentElement<>
// will replace the above mapper
// public static Action<IComponentMapper<Image>> Mapping()
public static Action<IComponentElementMapper<Image>> Mapping()
{
return c =>
{
c.Property(p => p.AltText);
c.Property(p => p.Name, m =>
{
m.Length(255);
});
c.Property(p => p.Path, m =>
{
m.Length(255);
});
c.Property(p => p.Height);
c.Property(p => p.Width);
c.Parent(x => x.Article, p => p.Access(Accessor.ReadOnly));
};
}
}
现在我们将传递它,,就像Adam Bar在此处记录Mapping-by-Code - Set and Bag 一样,映射到
// mapping for property:
// public virtual IList<Image> Images {get; set;}
Bag(x => x.Images // reference
, b => { } // bag properties mapper
, v => v.Component(ImageMap.Mapping()) // here we pass the <composite-element>
);
xml结果将符合预期:
<bag name="Images" ... >
<key column="ArticleId" ... />
<composite-element class="Occupation">
<parent name="Article" access="readonly" />
<property name="AltText" />
<property name="Name" length="255" />
...
</composite-element>
</bag>