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

发布于 2024-08-26 08:50:45 字数 1688 浏览 2 评论 0原文

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 类型

<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

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

I want to do the way the author suggested for fluent nhibernate many-to-many but use automapping instead of HBM file.

Here are my two entities

 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; }

}

Tried with HashSet, IList, and Set. But when I looked at the HBM files generated by calling automapping output method:

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

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

It's still bag type

<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>

Is there any conventions/override I can use in Fluent NHibernate to alter the collection type for all the ManyToMany in the app domain? I looked at IHasManyToMany convention but no clue.

Anyone can help? Thanks.

BTW, I'm using latest build in http://github.com/jagregory/fluent-nhibernate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-09-02 08:50:45

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

正如 James 在此 线程“自动映射器非常固执且不灵活,并且它需要集合以 IList 或 ISet 形式公开。”

Changing the type of Users and Cities from Set to ISet should solve your issue.

As stated by James in this thread, "The automapper is very opinionated and inflexible, and it expects collections to be exposed as IList or ISet."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文