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

发布于 2024-11-09 14:55:33 字数 3025 浏览 2 评论 0原文

我有这个映射器类。我不是延迟加载方面的专家,所以请您告诉我为什么有时它有效,有时却不起作用。 (位置的 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;

命名空间 Unde.Mergem.Repository.NHibernate.Mappings { /// 表示'Location'实体的映射,由'Location'类表示。 公共部分类 LocationMap : ClassMap { /// 初始化类的新实例。 公共位置地图() { 表("[dbo].[位置集]"); OptimisticLock.None(); 延迟加载();

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(); }

}


I have this mapper class. I'm no expert in lazy loading, so could you please enlighten me why sometimes it works and other times it doesn't. (The Id of the location is the problem)


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;
    }
}

The mapping class:

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(); }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2024-11-16 14:55:33

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

看一下:

何时应该避免使用 NHibernate 的延迟加载功能?

If I can give you a suggestion I would avoid to use lazy loading in web enviroments as it decrease the performance.

have a look at:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文