2012-01-12 63 views
4

我是一個NHibernate的新手試圖用流利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不錯TESTDATA ^^ – Firo 2012-01-12 13:29:52

回答

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

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

應該

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

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

應該,謝謝! – 2012-01-12 13:41:14