如何使用/映射数据库视图来填充包含的集合?

时间:2009-07-10 15:52:06

标签: c# fluent-nhibernate nhibernate-mapping

我有这些课程:

public class FloorFill
{
    protected FloorFill(){}
    public virtual ProductCatalog Catalog { get; set; }
    public virtual Inventory BatchedItem { get; set; }
    public virtual Transaction Batch { get; set; }
    public virtual int ItemReference { get; set; }
    public virtual IList<InventoryLocation> BackstockLocations { get; set; }
}
public class InventoryLocation
{
    public InventoryLocation(){}
    public virtual int Id { get; set; }
    public virtual int ItemReference { get; private set; }
    public virtual Location Where { get; set; }
    public virtual int HowMany { get; set; }
}

我有一个数据库视图,按位置聚合Items和其他一些过滤。我想在映射中引用此视图来填充FloorFill.BackstockLocations集合。

我应该使用什么方法来填充此集合?我想收集延迟加载,但此时,我很乐意获取数据。

以下是映射文件:

public class FloorFillMap : EntityBaseMap<FloorFill>
{        
    public FloorFillMap()
    {
        Map(x => x.ItemReference);
        References(x => x.Catalog, "ProductCatalogId")
                        .WithForeignKey();
        References(x => x.Batch, "TransactionId")
                .WithForeignKey()
                .Cascade.SaveUpdate();
        References(x => x.BatchedItem, "InventoryId")
                .WithForeignKey()
                .Cascade.SaveUpdate();
        HasMany(x => x.BackstockLocations)
            .KeyColumnNames.Add("ItemReference")
            .Inverse()
            .Cascade.None()
            .LazyLoad();
    }
}

public class InventoryLocationMap : ClassMap<InventoryLocation>
{
    public InventoryLocationMap ()
    {
        WithTable("InventoryLocations");
        Id(x => x.Id);
        References(x => x.Where, "LocationId")
            .FetchType.Join()
            .Cascade.None();
        Map(x => x.HowMany);
        ReadOnly();
    }
}

生成的查询是:

SELECT 
    this_.Id as Id33_0_, 
    this_.Created as Created33_0_, 
    this_.ItemReference as ItemRefe3_33_0_, 
    this_.Modified as Modified33_0_, 
    this_.RowVersion as RowVersion33_0_, 
    this_.ProductCatalogId as ProductC6_33_0_, 
    this_.TransactionId as Transact7_33_0_, 
    this_.InventoryId as Inventor8_33_0_ 
FROM [FloorFill] this_

1 个答案:

答案 0 :(得分:4)

映射视图与映射表相同,只要您不尝试更新它即可。

你在这句话中想做什么?

References(x => x.Where, "LocationId")
  .FetchType.Join().WithColumns("Id").Cascade.None();

"LocationId"是关键列名称,但WithColumns调用将覆盖该值。

发生或未发生的错误或其他迹象会有所帮助。