如何映射 IDictionary<string, Entity>在流畅的 NHibernate 中 [英] How to map IDictionary&lt;string, Entity&gt; in Fluent NHibernate

查看:24
本文介绍了如何映射 IDictionary<string, Entity>在流畅的 NHibernate 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 IDictionary 的类.

I have an class with an IDictionary on it.

  <map name="CodedExamples" table="tOwnedCodedExample">
    <key>
      <column name="OwnerClassID"/>
    </key>
    <index type="string" column="ExampleCode"/>
    <many-to-many class="CodedExample" column ="CodedExampleClassID"/>
  </map>

如您所见,它使用多对多从他们的表中获取 CodedExamples,使用 tOwnedCodedExample 表来查找所有者类拥有的那些.

as you can see it uses a many-to-many to get the CodedExamples from their table using the tOwnedCodedExample table to find which are owned by the OwnerClass.

我意识到这是一个非常基本的(希望是标准的)映射,但我很挣扎,找不到任何相关文档,因此非常感谢任何可能的帮助.

I realise that this is a very basic (and hopefully standard) mapping but am struggling and can't find any documentation for it, therefore would be very grateful of any help possible.

非常感谢

斯图

推荐答案

我有一个可行的例子,你应该很清楚.

I have a working example, this should make it clear to you.

课程:

public class Customer : Entity
{        
    public IDictionary<string, Book> FavouriteBooks { get; set; }
}

public class Book : Entity
{
    public string Name { get; set; }
}

然后是地图:

HasManyToMany<Book>(x => x.FavouriteBooks)
            .Table("FavouriteBooks")                
            .ParentKeyColumn("CustomerID")
            .ChildKeyColumn("BookID")
            .AsMap<string>("Nickname")                
            .Cascade.All();

结果xml:

<map cascade="all" name="FavouriteBooks" table="FavouriteBooks" mutable="true">
  <key>
    <column name="`CustomerID`" />
  </key>
  <index type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <column name="`Nickname`" />
  </index>
  <many-to-many class="Domain.Book, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <column name="`BookID`" />
  </many-to-many>
</map>

生成的 SQL:

create table "Customer" (
    "Id"  integer,
   "FirstName" TEXT,
   primary key ("Id")
)

create table FavouriteBooks (
    "CustomerID" INTEGER not null,
   "BookID" INTEGER not null,
   "Nickname" TEXT not null,
   primary key ("CustomerID", "Nickname")
)

create table "Book" (
    "Id"  integer,
   "Name" TEXT,
   primary key ("Id")
)

这篇关于如何映射 IDictionary<string, Entity>在流畅的 NHibernate 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆