功能NHibernate - HasManyToMany NHibernate.MappingException:重复列映射集合 [英] Fluent NHibernate - HasManyToMany NHibernate.MappingException: Repeated column in mapping for collection

查看:209
本文介绍了功能NHibernate - HasManyToMany NHibernate.MappingException:重复列映射集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手NHibernate的尝试配置与功能NHibernate现有的数据库。问题是与一个多到多的映射,在这个例子再通过库和书籍psented $ P $。我想这应该是非常基本的东西,但我得到了以下异常:

  FluentNHibernate.Cfg.FluentConfigurationException:创建一个SessionFactory无效或不完整的配置使用。检查PotentialReasons收集和的InnerException了解更多详情。

 ---> NHibernate.MappingException:重复列的集合映射:MvcNhibernatePoc.Models.Book.Libraries柱:BOOKID
 

数据库的结构不应改变,看起来是这样的:

 表**书**
BOOKID(INT)
bookname的(VARCHAR(255))

表** **库
LibraryId(INT)
库名称(VARCHAR(255))

表** Book_Library **
ID(INT)
BOOKID(INT)
LibraryId(INT)
 

在此基础上我已经创建了以下领域类:

 公共类图书
    {
        公共虚拟INT LibraryId {获得;组; }
        公共虚拟字符串名称{;组; }

        公共虚拟IList的<图书>图书{获得;组; }

        公共图书馆()
        {
            图书=新的名单,其中,图书>();
        }
    }


公共类图书
{
    公共虚拟INT BOOKID {获得;组; }
    公共虚拟字符串名称{;组; }

    公共虚拟IList的<图书馆>库{获得;组; }

    公共图书()
    {
        库=新的名单,其中,图书馆>();
    }
}
 

映射:

 公共类LibraryMap:ClassMap<图书馆>
{
    公共LibraryMap()
    {
        表(库);
        ID(L => l.LibraryId).COLUMN(LibraryId);
        地图(L => l.Name).COLUMN(库名称);

        HasManyToMany<图书>(L => l.Books)
            。表(Book_Library)
            .ParentKeyColumn(LibraryId)
            .ChildKeyColumn(LibraryId)
            .Cascade.SaveUpdate();
    }
}

公共类BookMap:ClassMap<图书>
{
    公共BookMap()
    {
        表(尚书);
        ID(B => b.BookId).COLUMN(BOOKID);
        地图(B => b.Name).COLUMN(bookname的);

        HasManyToMany<图书馆>(B => b.Libraries)
            。表(Book_Library)
            .ParentKeyColumn(BOOKID)
            .ChildKeyColumn(BOOKID)
            .Cascade.SaveUpdate()
            。逆();
    }
}
 

流利的配置:

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

最后我失败的测试code:

  VAR库=新库{LibraryId = 1,名称=亚历山大图书馆};
VAR书=新的图书{BOOKID = 1,名称=金字塔傻瓜};

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

使用(VAR会话= NHibernateHelper.OpenSession())
{
    使用(VAR交易= session.BeginTransaction())
    {
        的session.save(库);
        session.Flush();

        器transaction.commit();

        Console.WriteLine(保存库+ library.Name);
    }
}
 

解决方案

  .ParentKeyColumn(BOOKID)
        .ChildKeyColumn(BOOKID)

        .ParentKeyColumn(LibraryId)
        .ChildKeyColumn(LibraryId)
 

  // BookMap
        .ParentKeyColumn(BOOKID)
        .ChildKeyColumn(LibraryId)

        // LibraryMap
        .ParentKeyColumn(LibraryId)
        .ChildKeyColumn(BOOKID)
 

I'm a NHibernate novice trying to configure an existing database with Fluent NHibernate. The problem is with a many-to-many mapping, in this example represented by libraries and books. I guess this should be really basic stuff, but I get the following exception:

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

The structure of the database should not be changed and looks something like:

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

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

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

Based on this I have created the following domain classes:

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

Mapping:

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

Fluent configuration:

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

And finally my failing 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);
    }
}

解决方案

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

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

should be

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

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

这篇关于功能NHibernate - HasManyToMany NHibernate.MappingException:重复列映射集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆