c# - 流利的 NHibernate - HasManyToMany NHibernate.MappingException : Repeated column in mapping for collection

标签 c# .net nhibernate fluent

我是一名 NHibernate 新手,正在尝试使用 Fluent NHibernate 配置现有数据库。问题在于多对多映射,在本例中以图书馆和书籍为代表。我想这应该是非常基本的东西,但我得到以下异常:

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();

最后是我失败的测试代码:

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);
    }
}

最佳答案

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

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

应该是

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

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

关于c# - 流利的 NHibernate - HasManyToMany NHibernate.MappingException : Repeated column in mapping for collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835658/

相关文章:

c# - 在运行时创建具有不同资源的 exe 副本

c# - 开始使用 Telerik : kendoDropDownList is not a function

.net - 如何根据传递给 VB.NET 泛型方法的类型执行条件逻辑

c# - 不确定如何使用 LINQ 实现对 nHibernate 的特定扩展

nhibernate - 在 NHibernate 3 中使用 Linq 时急切加载

javascript - 从 Razor 表单提交中调用 JS 方法

C# Web浏览器点击Javascript按钮

c# - 数据表和 AutoGeneratedColummns 中带有枚举的 WPF DataGrid ="True"

c# - 转换带有千位分隔符的格式化十进制字符串

asp.net-mvc-3 - 抛弃 ActiveRecord 和 NHibernate——如何重新架构?