2012-01-12 11 views
4

私はNHibernateのFluent NHibernateで既存のデータベースを設定しようとしています。この問題は、ライブラリと書籍で表現された多対多マッピングの問題です。私はこれは本当に基本的なものであるべきと思いますが、私は次の例外を取得:流暢NHibernate - HasManyToMany NHibernate.MappingException:コレクションのマッピングの繰り返し列

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. 

---> NHibernate.MappingException: Repeated column in mapping for collection: MvcNhibernatePoc.Models.Book.Libraries column: BookId 

データベースの構造を変更してのようになりますすべきではない:私が作成したこのことに基づいて

Table **Book** 
BookId (int) 
BookName (varchar(255)) 

Table **Library** 
LibraryId (int) 
LibraryName (varchar(255)) 

Table **Book_Library** 
Id (int) 
BookId (int) 
LibraryId (int) 

以下のドメインクラス:

public class Library 
    { 
     public virtual int LibraryId { get; set; } 
     public virtual string Name { get; set; } 

     public virtual IList<Book> Books { get; set; } 

     public Library() 
     { 
      Books = new List<Book>(); 
     } 
    } 


public class Book 
{ 
    public virtual int BookId { get; set; } 
    public virtual string Name { get; set; } 

    public virtual IList<Library> Libraries { get; set; } 

    public Book() 
    { 
     Libraries = new List<Library>(); 
    } 
} 

マッピング:

public class LibraryMap : ClassMap<Library> 
{ 
    public LibraryMap() 
    { 
     Table("Library"); 
     Id(l => l.LibraryId).Column("LibraryId"); 
     Map(l => l.Name).Column("LibraryName"); 

     HasManyToMany<Book>(l => l.Books) 
      .Table("Book_Library") 
      .ParentKeyColumn("LibraryId") 
      .ChildKeyColumn("LibraryId") 
      .Cascade.SaveUpdate(); 
    } 
} 

public class BookMap : ClassMap<Book> 
{ 
    public BookMap() 
    { 
     Table("Book"); 
     Id(b => b.BookId).Column("BookId"); 
     Map(b => b.Name).Column("BookName"); 

     HasManyToMany<Library>(b => b.Libraries) 
      .Table("Book_Library") 
      .ParentKeyColumn("BookId") 
      .ChildKeyColumn("BookId") 
      .Cascade.SaveUpdate() 
      .Inverse(); 
    } 
} 

流暢設定:

Fluently.Configure() 
      .Database(MsSqlConfiguration.MsSql2008.ConnectionString(conString).ShowSql) 
      .Mappings(m => m.FluentMappings 
      .AddFromAssemblyOf<Library>()) 
      .BuildSessionFactory(); 

そして最後に私の失敗testcode:

var library = new Library { LibraryId = 1, Name = "Alexandria library"}; 
var book = new Book { BookId = 1, Name = "Pyramids for dummies" }; 

library.Books.Add(book); 
book.Libraries.Add(library); 

using (var session = NHibernateHelper.OpenSession()) 
{ 
    using (var transaction = session.BeginTransaction()) 
    { 
     session.Save(library); 
     session.Flush(); 

     transaction.Commit(); 

     Console.WriteLine("Saved library " + library.Name); 
    } 
} 
+0

+1でなければなりません^^ – Firo

答えて

8
 .ParentKeyColumn("BookId") 
     .ChildKeyColumn("BookId") 

     .ParentKeyColumn("LibraryId") 
     .ChildKeyColumn("LibraryId") 

はいいテストデータ用

 // BookMap 
     .ParentKeyColumn("BookId") 
     .ChildKeyColumn("LibraryId") 

     // LibraryMap 
     .ParentKeyColumn("LibraryId") 
     .ChildKeyColumn("BookId") 
+0

それは、あなたに感謝すべきです! –

関連する問題