나는 아이 엔티티 섹션의 컬렉션을 포함하는 다음 부모 개체 부서자 NHibernate는 : 부모 속성에 자식 개체를 투사하면 예외를 발생
public class Department
{
private Iesi.Collections.Generic.ISet<Section> _sections;
public Department()
{
_sections = new HashedSet<Section>();
}
public virtual Guid Id { get; protected set; }
public virtual string Name { get; set; }
public virtual ICollection<Section> Sections
{
get { return _sections; }
}
public virtual int Version { get; set; }
}
public partial class Section
{
public Section()
{
_employees = new HashedSet<Employee>();
}
public virtual Guid Id { get; protected set; }
public virtual string Name { get; set; }
public virtual Department Department { get; protected set; }
public virtual int Version { get; set; }
}
나는 다음과 같은 DTO
로 변환 (평평)하고 싶은이public class SectionViewModel
{
public string DepartmentName { get; set; }
public string SectionName { get; set; }
}
다음 코드를 사용하십시오.
SectionModel sectionModel = null;
Section sections = null;
var result = _session.QueryOver<Department>().Where(d => d.Company.Id == companyId)
.Left.JoinQueryOver(x => x.Sections,() => sections)
.Select(
Projections.ProjectionList()
.Add(Projections.Property<Department>(d => sections.Department.Name).WithAlias(() => sectionModel.DepartmentName))
.Add(Projections.Property<Department>(s => sections.Name).WithAlias(() => sectionModel.SectionName))
)
.TransformUsing(Transformers.AliasToBean<SectionModel>())
.List<SectionModel>();
그러나 나는 다음과 같은 예외가 점점 오전 : 나는 심지어 다음 LINQ 표현
var result = (from d in _session.Query<Department>()
join s in _session.Query<Section>()
on d.Id equals s.Department.Id into ds
from sm in ds.DefaultIfEmpty()
select new SectionModel
{
DepartmentName = d.Name,
SectionName = sm.Name ?? null
}).ToList();
매핑을 시도 Domain.Section
: Department.Name의 : 재산 확인할 수 없음
public class DepartmentMap : ClassMapping<Department>
{
public DepartmentMap()
{
Id(x => x.Id, m => m.Generator(Generators.GuidComb));
Property(x => x.Name,
m =>
{
m.Length(100);
m.NotNullable(true);
});
Set(x => x.Sections,
m =>
{
m.Access(Accessor.Field);
m.Inverse(true);
m.BatchSize(20);
m.Key(k => { k.Column("DeptId"); k.NotNullable(true); });
m.Table("Section");
m.Cascade(Cascade.All | Cascade.DeleteOrphans);
},
ce => ce.OneToMany());
}
}
public class SectionMap : ClassMapping<Section>
{
public SectionMap()
{
Id(x => x.Id, m => m.Generator(Generators.GuidComb));
Property(x => x.Name,
m =>
{
m.Length(100);
m.NotNullable(true);
});
ManyToOne(x => x.Department,
m =>
{
m.Column("DeptId");
m.NotNullable(true);
});
}
}
하지만이 메서드 또는 작업을 구현하지 않습니다을 throw합니다.
내가 잘못했거나 누락 된 부분에 대한 안내를 구하십시오.
섹션에서 부서로 명시 적으로 참여해야합니다 ... 아니면 '부서명. 이름'을 선택할 수도 있습니다. SQL이 어떻게 생겼는지 알고 있습니까? –
다음은 예상되는 SQL'select d.Name을 deptName으로, s.Name을 SectionName으로, Dept d를 왼쪽 조인 섹션 s에 d.id = s.deptid 순서로 d.Name로 오름차순으로 정렬합니다.이름 오름차순 ' – kagundajm