10

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Stu
  • 2,426
  • 2
  • 26
  • 42
  • 3
    Its not a duplicate. The questions are different. – Paul Batum Feb 13 '10 at 09:12
  • 2
    Thanks Paul, the questions are very different. My other question is about a composite key for the dictionary index, this - I hope - is a much more simple question about standard usage of dictionaries in FluentNH. Paul, do you know how to map this? I've seen plenty of posts saying that it's possible but none that show how! – Stu Feb 13 '10 at 14:28

2 Answers2

13

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")
)
Paul Batum
  • 8,165
  • 5
  • 40
  • 45
  • That looks great thanks. I'll give it a go tomorrow when I get into the office and get back to you. – Stu Feb 14 '10 at 11:05
  • Thanks Paul, that worked perfectly. Unfortunately this question has been closed as a duplicate, but hopefully it'll still appear in search results so others can use it as an example. Cheers Stu – Stu Feb 15 '10 at 20:10
2

Map the book.name as the key instead of the nickname.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
kmoo01
  • 43
  • 2