简体   繁体   English

如何使用流利的hibernate保存对象

[英]How to save objects with fluent nhibernate

Hi am confused with how to save object with fluent nhibernate. 您好,我对如何使用流利的nhibernate保存对象感到困惑。

say I have a class Foo which has one to many relationship with ChildFoo. 说我有一个Foo类,它与ChildFoo有一对多的关系。

Table foo has id and name. 表foo具有ID和名称。 Table ChildFoo has sourceID which matches the id for the foo entity. 表ChildFoo的sourceID与foo实体的ID相匹配。

The mapping would look like this. 映射看起来像这样。

class Foo 
{
   public virtual ID {get;set;}
   public IList<FooChild> Components{get;set;}
}

the mapping would look like this 映射看起来像这样

public FooMap : ClassMap<FooMap> 
{
   public FooMap()
   {
      HasMany(x => x.Components).KeyColumn("SourceID");
   }
}

Now when I want to save FooMap with some collection of Components, how do I tell fluent nhibernate to save the entity ( a class foo with a bunch of Foo Children stored in the Components list) I have created, without me requiring me to save each component manually. 现在,当我想用​​一些组件集合保存FooMap时,如何告诉fluent nhibernate保存我创建的实体(一个foo类,在组件列表中存储了一堆Foo Children),而无需我保存每个实体手动组件。

thanks. 谢谢。

You'd want to change your HasMany mapping to the following: 您想要将HasMany映射更改为以下内容:

HasMany(x => x.Components).KeyColumn("SourceID").Inverse().Cascade.All();

Also it looks like your mapping is incorrect. 而且看起来您的映射不正确。 It should be this: 应该是这样的:

public FooMap : ClassMap<Foo> 
{
   public FooMap()
   {
      Id(x => x.ID, "SourceID");
      HasMany(x => x.Components)
         .KeyColumn("SourceID")
         .Inverse()
         .Cascade.All();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM