一个简单的延迟加载问题?流利的NHibernate

问题描述

| 我有这个Mapper班。我不是延迟加载专家,所以请您启发我为什么有时有时会起作用,而有时却不起作用。 (位置的ID是个问题)
public static class LocationMapper
{

    public static IEnumerable<DropDownItemView> ConvertToLocationViews
        (this IEnumerable<Location> Locations)
    {
        return Mapper.Map<IEnumerable<Location>,IEnumerable<DropDownItemView>>(Locations);
    }

    public static LocationFewDetailsView ConvertToLocationFewDetailsView(this Location loc)
    {
        LocationFewDetailsView location = new LocationFewDetailsView();
        location.CityName = loc.City.Name; //The lazy loading works here
        location.LocationId = loc.Id; // *But not here. The id is 0. What Could be the problem?*
        location.LocationName = loc.Name; //The lazy loading works here
        return location;
    }
}
映射类: 使用系统; 使用System.Collections.Generic; 使用System.Text; 使用FluentNHibernate.Mapping; 使用Unde.Mergem.Model.EntityClasses; 命名空间Unde.Mergem.Repository.NHibernate.Mappings {     ///表示\'Location \'实体的映射,由\'Location \'类表示。     公共局部类LocationMap:ClassMap     {         ///初始化该类的新实例。         公共LocationMap()         {             Table(\“ [dbo]。[LocationSet] \”);             OptimisticLock.None();             LazyLoad();
        Id(x=>x.Id)
            .Access.CamelCaseField(Prefix.Underscore)
            .Column(\"[Id]\")
            .GeneratedBy.Identity();
        Map(x=>x.Address).Column(\"[Address]\").Not.Nullable().Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.Capacity).Column(\"[Capacity]\").Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.Description).Column(\"[Description]\").Not.Nullable().Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.Map).CustomType(\"BinaryBlob\").Column(\"[Map]\").Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.MapUrl).Column(\"[MapURL]\").Not.Nullable().Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.Name).Column(\"[Name]\").Not.Nullable().Access.CamelCaseField(Prefix.Underscore);
        Map(x=>x.Website).Column(\"[Website]\").Not.Nullable().Access.CamelCaseField(Prefix.Underscore);

        References(x=>x.City)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.All()
            .Fetch.Select()
            .Columns(\"[CityId]\");
        HasMany(x=>x.Events)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.AllDeleteOrphan()
            .Fetch.Select()
            .Inverse()
            .LazyLoad()
            .KeyColumns.Add(\"[LocationId]\");
        References(x=>x.Host)
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.All()
            .Fetch.Select()
            .Columns(\"[HostId]\");

        AdditionalMappingInfo();
    } 

    /// <summary>Partial method for adding additional mapping information in a partial class.</summary>
    partial void AdditionalMappingInfo();
} 
}     

解决方法

如果可以提出建议,我会避免在网络环境中使用延迟加载,因为这会降低性能。 看一下: 什么时候应该避免使用NHibernate的延迟加载功能?