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

时间:2011-05-26 15:53:01

标签: c# asp.net-mvc fluent-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;
    }
}

地图类:

using System;
using System.Collections.Generic;
using System.Text;
using FluentNHibernate.Mapping;
using Unde.Mergem.Model.EntityClasses;

namespace Unde.Mergem.Repository.NHibernate.Mappings { /// Represents the mapping of the 'Location' entity, represented by the 'Location' class. public partial class LocationMap : ClassMap { /// Initializes a new instance of the class. public 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(); }

}


1 个答案:

答案 0 :(得分:0)

如果我能给你一个建议,我会避免在网络环境中使用延迟加载,因为它会降低性能。

看看:

When should one avoid using NHibernate's lazy-loading feature?