nhibernate中值对象的映射列表

Mapping list of value objects in nhibernate

我有模型房屋,其中有门列表(门代表值对象)

public class House : Entity<Guid>
{
   public int Id { get; set; }
   public List<Door> Doors { get; set; }
   ...
   public House(){
      Doors = new List<Door>();
   }
}

我正在通过代码方法使用 nhibernate 映射,所以我尝试映射 House

HouseMap.cs

public class HouseMap: ClassMapping<House>
{
   public HouseMap()
   {
      ...
      Component(c => c.Doors, DoorMap.Mapping());
   }
}

DoorMap.cs

public class DoorMap
{
    public static Action<IComponentMapper<Door>> Mapping()
    {
        return c =>
        {
            c.Property(p => p.Number);
            c.Property(p => p.Color);             
        };
    }
}

我在 HouseMap.cs

上遇到错误

Component(c => c.Doors, DoorMap.Mapping());

CANNOT CONVERT LAMBDA EXPRESSION TO TYPE 'STRING' BEACUSE IT IS NOT A DELEGATE TYPE

我做错了什么?非列表值对象的其他映射都可以。

您一定要为您的 C# 实体使用接口:

public class House : Entity<Guid>
{
    ...
    //public List<Door> Doors { get; set; }
    public virtual IList<Door> Doors { get; set; }

这里描述了参考集的映射:

Mapping-by-Code - Set and Bag 亚当·巴尔

基于此,Doors 集合映射应该如下所示 (我更喜欢 .Bag()IList,而不是 .Set()更具体一点 ISet)

Bag(x => x.Doors, c =>
{
   ...

这些是其他可用设置 (摘自上面的 link):

c.Fetch(CollectionFetchMode.Join); // or CollectionFetchMode.Select
                                   // , CollectionFetchMode.Subselect
c.BatchSize(100);
c.Lazy(CollectionLazy.Lazy); // or CollectionLazy.NoLazy, CollectionLazy.Extra

c.Table("tableName");
c.Schema("schemaName");
c.Catalog("catalogName");

c.Cascade(Cascade.All);
c.Inverse(true);

c.Where("SQL command");
c.Filter("filterName", f => f.Condition("condition"));
c.OrderBy(x => x.Name); // or SQL expression

c.Access(Accessor.Field);
c.Sort<CustomComparer>();
c.Type<CustomType>();
c.Persister<CustomPersister>();
c.OptimisticLock(true);
c.Mutable(true);
...