Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

c# - How to map IDictionary<string, Entity> in Fluent NHibernate

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>

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.

Many Thanks

Stu

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Classes:

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

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

And then the map:

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

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

Generated 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")
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...