【发布时间】:2011-05-26 15:53:00
【问题描述】:
我有这个映射器类。我不是延迟加载方面的专家,所以请您告诉我为什么有时它可以工作,而有时却不能。 (位置的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 { /// 初始化该类的一个新实例。 公共位置图() { Table("[dbo].[LocationSet]"); 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();
}
}
【问题讨论】:
-
了解异常或具体问题会很有帮助。
-
也不例外。 "loc.Id" 不返回 corect id (它返回 0) 。 ConvertToLocationFewDetailsView(this Location loc) 的参数是 Castle.Proxies.LocationProxy 类型。 loc.id 不应该是我的位置类型的“真实”对象的 id 吗? (我希望我说清楚了)
-
你能在
loc.Name上查看生成的sql,它是否也选择了id?换行loc.Id和loc.Name时Id还是0吗?
标签: c# asp.net-mvc fluent-nhibernate