Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 473089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:09:06+00:00 2026-05-13T00:09:06+00:00

It appears that NHibernate cannot automap more than one IList of a given type

  • 0

It appears that NHibernate cannot automap more than one IList of a given type in an entity.

Consider the following two entities (based on the Examples.FirstProject sample code that is included with the Fluent NHibernate source code).

public class Employee
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class Store
{
    public virtual int Id { get; private set; }
    public virtual IList<Employee> Staff { get; set; }
    public virtual IList<Employee> Managers { get; set; }
}

This seems to be a perfectly valid object model – each store has several staff employees and several manager employees.

But when I automap, the Staff and Managers lists are stored in the Employee table,all with the same foreign key.

Employee Table

Id FirstName LastName Store_id 
3  Daisy     Harrison   1 
4  Jack      Torrance   1 
5  Sue       Walkters   1 
6  Tom       Tommorow   1 
7  Dick      Diggler    1 

The net result is that when the data is read back out of the database, both Staff and Managers lists are populated with every row in the table.

This looks like a bug in Automapping to me, but I’m fairly new to NHibernate in any form, and don’t fully know it’s limitations yet.

In any case, how can I make NHibernate treat the two lists as distinct?

If possible, I’d appreciate an Automapping code fragment that directly addresses the sample code I’ve provided (e.g. something like “put this exact override in the .Mappings section of your CreateSessionFactory”).

This is because I’m only somewhat familiar with Automapping, and not at all familiar with the older ways of doing things, which means I can’t “fill in the blanks” very well yet.

But if you only have time to point me in the right direction, that would be helpful too.

Here’s my CreateSessionFactory code, to give some context:

    private static ISessionFactory CreateSessionFactory()
    {
        ISessionFactory sessionFactory = null;

        const string autoMapExportDir = "AutoMapExport";
        if( !Directory.Exists(autoMapExportDir) )
            Directory.CreateDirectory(autoMapExportDir);

        try
        {
            var autoPersistenceModel = 
                AutoMap.AssemblyOf<Product>()
                        .Where(t => t.Namespace == "Examples.FirstProject.Entities")
                        .Conventions.Add( DefaultCascade.All() )
                ;

            sessionFactory = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard
                              .UsingFile(DbFile)
                              .ShowSql()
                         )
                .Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
                                             .ExportTo(autoMapExportDir)
                         )
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory()
                ;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        return sessionFactory;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:09:07+00:00Added an answer on May 13, 2026 at 12:09 am

    Paul Batum answered my question here, and provided a standalone working example here (click the Download button after you navigate to the linked page).

    The following code is copied from his answer. The key point is in the StoreMap class at the end of the listing, which sets up an override with a Where clause that uses the IsManager property in Employee.

    Note that (at least with v. 1.0.0.594) there is one big gotcha with Automapping – the mapping class (e.g. StoreMap) cannot be in the same Namespace as the domain class (e.g. Store)!

    Otherwise, NHibernate will throw “NHibernate.MappingException:
    (XmlDocument)(2,4): XML validation error: …”
    , with
    absolutely no indication of what or where the real problem is.

    This is probably a bug that may be fixed in later versions of Fluent NHibernate.

    public class Employee 
    { 
        public virtual int Id { get; private set; } 
        public virtual string FirstName { get; set; } 
        public virtual string LastName { get; set; } 
        public virtual bool IsManager { get; set; } 
    } 
    
    
    public class Store 
    { 
        public virtual int Id { get; private set; } 
        public virtual IList<Employee> Staff { get; private set; } 
        public virtual IList<Employee> Managers { get; private set; } 
    
    
        public Store() 
        { 
            Staff = new List<Employee>(); 
            Managers = new List<Employee>(); 
        } 
    
    
        public void AddManager(Employee employee) 
        { 
            employee.IsManager = true; 
            this.Managers.Add(employee); 
        } 
    
    
        public void AddStaff(Employee employee) 
        { 
            this.Staff.Add(employee); 
        } 
    
    
    } 
    

    Here is the mapping override for store:

    // Must be in different Namespace from class Store!!!
    public class StoreMap : IAutoMappingOverride<Store> 
    { 
       public void Override(AutoMapping<Store> mapping) 
       { 
           mapping.HasMany(x => x.Managers) 
               .Cascade.All() 
               .Where("(IsManager = 1)"); 
           mapping.HasMany(x => x.Staff) 
               .Cascade.All() 
               .Where("(IsManager = 0)"); 
       } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that when I do git stash apply, I need to then type
It appears that we will have to build/deploy one of our new JBoss apps
It appears that calling Html.RenderAction in Asp.Net MVC2 apps can alter the mime type
I'm using nhibernate and it appears that changes to my new object are not
From my personal research, it appears that OData implementations depend extensively on MS Entity
I have several one-to-many mappings in an NHibernate project that are configured to use
Appears that WAS does not support the integration binding. I've tried setting it up
It appears that second windows are not closing. Here is what I am doing....
It appears that add_to_class() https://github.com/django/django/blob/master/django/db/models/base.py#L218 doesn't actually add a column, but adds a field
It appears that a lot of software, including low-level system calls, etc., relies on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.