本文介绍了流利的nhibernate组件一对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class A
{
public virtual Guid Id {get;私人设置}
public virtual ComponentClass Component {get;组; }
}
公共类ComponentClass
{
公共虚拟IList< B>元素{get; set; }
}
public class B
{
public virtual Guid Id {get;私人设置}
public virtual DateTime Time {get;组; }
$ b我使用如下流畅的映射来映射它们:
public class AMap:ClassMap< A>
{
public A(){
Id(x => x.Id);
Component(x => x.Component,
c => c.HasMany(x => x.Elements).Inverse()。Cascade.All());
}
}
public class BMap:ClassMap< B>
{
public B(){
Id(x => x.Id);
Map(x => x.Time);
$ b当我保存实体时,如预期的那样将一张桌子和一张B桌子转移到另一桌子上
但是我有Component_id列中的空值。
您能告诉我我在这里丢失了什么吗?
解决方案如果您有直接的一对多关联到一个组件的集合(即没有 ComponentClass
包装器),那么你可以直接映射它:
HasMany(x => x.Elements)
.AsSet()
.Table(ElementTable)
.KeyColumn(KeyColumn )
.Cascade.All()
.Component(x =>
{
x.Map(c => c.Id);
x。 Map(c => c.Time);
})
.LazyLoad();
I have couple of classes and want to map them correctly to database:
public class A
{
public virtual Guid Id { get; private set; }
public virtual ComponentClass Component { get; set; }
}
public class ComponentClass
{
public virtual IList<B> Elements { get;set; }
}
public class B
{
public virtual Guid Id { get; private set; }
public virtual DateTime Time { get; set; }
}
I map them using fluent mappings like that:
public class AMap : ClassMap<A>
{
public A() {
Id(x => x.Id);
Component(x => x.Component,
c => c.HasMany(x => x.Elements).Inverse().Cascade.All());
}
}
public class BMap : ClassMap<B>
{
public B() {
Id(x => x.Id);
Map(x => x.Time);
}
}
When I save my entity, I have class A mapped to one table and class B to another as expected.But I have nulls in Component_id column.Can you tell me what am I missing here?
解决方案 If you have a one-to-many association direct to a collection of components (ie. without the ComponentClass
wrapper as per the question) then you can map it directly:
HasMany(x => x.Elements)
.AsSet()
.Table("ElementTable")
.KeyColumn("KeyColumn")
.Cascade.All()
.Component(x =>
{
x.Map(c => c.Id);
x.Map(c => c.Time);
})
.LazyLoad();
这篇关于流利的nhibernate组件一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!