如何使用Set而不是Bag设置Fluent NHibernate多对多自动化?

时间:2010-03-19 19:23:21

标签: fluent-nhibernate

http://www.codinginstinct.com/2010/03/nhibernate-tip-use-set-for-many-to-many.html

我想按照作者建议的方式进行流畅的nhibernate多对多,但使用自动代替HBM文件。

这是我的两个实体

 public class User
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Iesi.Collections.Generic.Set<City> Cities { get; set; }

}

 public class City{
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Iesi.Collections.Generic.Set<User> Users { get; set; }

}

尝试使用HashSet,IList和Set。但是当我查看通过调用自动化输出方法生成的HBM文件时:

 var autoMappings = new AutoPersistenceModel().AddEntityAssembly(entityAssembly).Where(x => x.Namespace.EndsWith("Domain"));

autoMappings.WriteMappingsTo((@"C:\TEMP");

它还是包型

<bag inverse="true" name="Users" table="MNUserCity" mutable="true">
      <key>
        <column name="CityId" />
      </key>
      <many-to-many class="MyApp.Entity.Domain.User, MyApp.Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <column name="UserId" />
      </many-to-many>
    </bag>

我可以在Fluent NHibernate中使用任何约定/覆盖来改变应用程序域中所有ManyToMany的集合类型吗?我看了IHasManyToMany惯例,但没有任何线索。

任何人都可以提供帮助?感谢。

顺便说一句,我正在使用http://github.com/jagregory/fluent-nhibernate

中的最新版本

1 个答案:

答案 0 :(得分:1)

UsersCities的类型从Set更改为ISet可以解决您的问题。

正如Jamesthread所述,“自动播放器非常固执,缺乏灵活性,并且预计集合将作为IList或ISet公开。”