我正在使用FluentNHibernate和Linq To Nhibernate,这些实体(只有相关部分):
public class Player : BaseEntity<Player>
{
private readonly IList<PlayerInTeam> allTeams = new List<PlayerInTeam>();
public IEnumerable<Team> Teams
{
get
{
return from playerInTeam in allTeams
where playerInTeam.Roster.Match == null
select playerInTeam.Roster.Team;
}
}
}
public class PlayerInTeam : BaseEntity<PlayerInTeam>
{
public int PlayerNumber { get; set; }
public Player Player { get; set; }
public Position Position { get; set; }
public Roster Roster { get; set; }
}
public class Roster : BaseEntity<Roster>
{
private readonly IList<PlayerInTeam> players = new List<PlayerInTeam>();
public Team Team { get; set; }
public IEnumerable<PlayerInTeam> Players { get { return players; } }
}
public class Team : BaseEntity<Team>
{
private readonly IList<Roster> allRosters = new List<Roster>();
public Team(string name, Sex sex)
{
allRosters.Add(new Roster(this));
}
public Roster DefaultRoster
{
get { return allRosters.Where(r => r.Match == null).First(); }
}
}
和匹配的映射:
public class PlayerMap : ClassMap<Player>
{
public PlayerMap()
{
HasMany<PlayerInTeam>(Reveal.Member<Player>("allTeams"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class PlayerInTeamMap : ClassMap<PlayerInTeam>
{
public PlayerInTeamMap()
{
References(pit => pit.Player)
.Not.Nullable();
References(pit => pit.Roster)
.Not.Nullable();
}
}
public class RosterMap : ClassMap<Roster>
{
public RosterMap()
{
References(tr => tr.Team)
.Not.Nullable();
HasMany<PlayerInTeam>(Reveal.Member<Roster>("players"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class TeamMap : ClassMap<Team>
{
public TeamMap()
{
HasMany<Roster>(Reveal.Member<Team>("allRosters"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
我有这个存储库方法:
public IEnumerable<Player> PlayersNotInTeam(Team team)
{
return from player in Session.Linq<Player>()
where !player.Teams.Contains(team)
select player;
}
这给了我这个例外: NHibernate.QueryException:无法解析属性:团队:Emidee.CommonEntities.Player [.Where(NHibernate.Linq.NhQueryable`1 [Emidee.CommonEntities.Player],Quote( (播放器,)=&gt;(不(.Contains(player.Teams,p1,)))),)]
我查看了由Fluent NHibernate生成的关于Player的hbm文件,这是我得到的:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Emidee.CommonEntities.Player, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" table="Players">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="increment" />
</id>
<bag access="field.camelcase" cascade="all-delete-orphan" inverse="true" name="allTeams" mutable="true">
<key>
<column name="Player_id" not-null="true" />
</key>
<one-to-many class="Emidee.CommonEntities.PlayerInTeam, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" />
</bag>
</class>
</hibernate-mapping>
实际上,我在该文件中没有Player的Teams属性。
但我的映射不应该照顾这个吗? 您认为问题在哪里?
提前致谢
麦克
答案 0 :(得分:2)
从技术上讲,无法查看C-sharp中的方法体。所以FluentNhibernate也不能这样做。您必须应用不同的策略来模拟实体和/或自己生成映射。
答案 1 :(得分:1)
hbm.xml文件是您的映射。因此,只要您不通知NHibernate您的Player类具有Teams属性,NHibernate就不知道它。
接下来,为什么你有一个hbm.xml映射文件和一个使用Fluent的映射? (似乎hbm.xml文件优先级/在Fluent NH映射之前)。