How to map IDictionary in Fluent NHibernate

后端 未结 2 1298
南旧
南旧 2020-12-16 03:08

I have an class with an IDictionary on it.

  
    
      

        
相关标签:
2条回答
  • 2020-12-16 03:20

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

    0 讨论(0)
  • 2020-12-16 03:37

    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")
    )
    
    0 讨论(0)
提交回复
热议问题