0

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

}


3
  • It would be helpful to know the exception or specific problem. Commented May 26, 2011 at 16:19
  • There's no exception. "loc.Id" doesn't return the corect id (it returns 0) . The parameter from ConvertToLocationFewDetailsView(this Location loc) is of type Castle.Proxies.LocationProxy . Shouldn't loc.id be the id of my "real" object of type Location? (I hope I made myself clear) Commented May 26, 2011 at 17:01
  • can you check the generated sql on loc.Name, does it select the id too? Is Id still 0 when you switch line loc.Id and loc.Name ? Commented May 30, 2011 at 11:22

1 Answer 1

0

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.