Fluent NHibernate映射CompositeId并使用SetProjection进行查询
我有两个表(Section和SectionList),它们由多对多表(Membership)关联。由于多对多表中有一个额外的属性,因此我必须将其分解为自己的实体:Fluent NHibernate映射CompositeId并使用SetProjection进行查询,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我有两个表(Section和SectionList),它们由多对多表(Membership)关联。由于多对多表中有一个额外的属性,因此我必须将其分解为自己的实体: public MembershipMap() { UseCompositeId() .WithKeyReference(x => x.Section, "SectionId") .WithKeyReference(x => x.SectionL
public MembershipMap()
{
UseCompositeId()
.WithKeyReference(x => x.Section, "SectionId")
.WithKeyReference(x => x.SectionList, "SectionList");
Map(x => x.Status);
}
而SectionList映射如下:
public SectionListMap()
{
Id(x => x.Id)
.WithUnsavedValue(0);
HasMany(x => x.Memberships)
.Inverse()
.Cascade.AllDeleteOrphan();
}
这种关系似乎运作良好,但我尝试对其运行高级查询时除外。例如,以下是一个仅获取特定字段并转换为DTO的查询:
var criteria = DetachedCriteria.For<CustomSectionListMembership>()
.CreateAlias("SectionList", "sl")
.CreateAlias("Section", "s")
.CreateAlias("s.Website", "w")
.CreateAlias("w.Publisher", "p")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("s.Id"), "SectionId") //add projections for every propery you want returned
.Add(Projections.Property("s.Name"), "SectionName") // mapping entity name -> DTO name
.Add(Projections.Property("w.Id"), "WebsiteId")
.Add(Projections.Property("w.Name"), "WebsiteName")
.Add(Projections.Property("p.Id"), "PublisherId")
.Add(Projections.Property("p.Name"), "PublisherName")
.Add(Projections.Property("Status")))
.Add(Expression.Eq("sl.Id", listId))
.SetResultTransformer(Transformers.AliasToBean(typeof(MembershipDTO))); //transform to the DTO
var membership = repository.FindAll(criteria);
可能是什么问题?我似乎也遇到了同样的问题。你的问题解决了吗?我似乎也遇到了同样的问题。你的问题解决了吗?
SELECT s2_.SectionId as y0_,
s2_.Name as y1_,
w3_.WebsiteId as y2_,
w3_.Name as y3_,
p4_.PublisherId as y4_,
p4_.Name as y5_,
this_.Status as y6_ FROM Membership this_ WHERE csl1_.ListId = 6923 /* @p0 */