English 中文(简体)
2. 液化NHibern - HasMany ToMany NHibernate。 制图学: 收集绘图栏的重复
原标题:Fluent NHibernate - HasManyToMany NHibernate.MappingException: Repeated column in mapping for collection

I m a NHibernate novice试图将现有的数据库与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")
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签