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 3432218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:25:19+00:00 2026-05-18T07:25:19+00:00

Problem : There are searches that can be stored in the DB. Each search

  • 0

Problem:

There are searches that can be stored in the DB. Each search has a collection of filters. Also there are roles. Each role may have (nullable column) a default search assigned to it. Also, each search is visible to zero or many roles (many-to-many relationship).

When I try to access the search filters, NH tries to access filters.DefaultSearchId, which doesn’t exist in filters table.

DB:

CREATE TABLE [dbo].[Searches]
(
    Id int identity(1,1) primary key,
    Description nvarchar(2000) not null
);

CREATE TABLE [dbo].[Filters]
(
    Id int identity(1,1) primary key,
    Description nvarchar(2000) not null,
    SearchId int not null references Searches(Id)
);

CREATE TABLE [dbo].[Roles]
(
    Id int identity(1,1) primary key,
    Name nvarchar(255) not null,
    DefaultSearchId int null references Searches(Id)
);
CREATE TABLE [dbo].[SearchesRoles]
(
    SearchId int not null references Searches(Id),
    RoleId int not null references Roles(Id)
);

Entities:

  public class Search {
        public virtual int Id { get; set; }
        public virtual string Description { get; set; }
        public virtual ICollection<Filter> Filters { get; set; }
        public virtual ICollection<Role> Roles { get; set; }
    }

    public class Filter {
        public virtual int Id { get; set; }
        public virtual string Description { get; set; }
        public virtual Search Search { get; set; }
    }

    public class Role {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Search DefaultSearch { get; set; }
    }

Mappings:

 public class SearchMap : ClassMap<Search>{
        public SearchMap() {
            Table("Searches");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Description);
            HasMany(x => x.Filters).Inverse().Cascade.All().AsBag();
            HasManyToMany(x => x.Roles).Table("SearchesRoles").ParentKeyColumn("SearchId").ChildKeyColumn("RoleId");
        }
    }

 public class FilterMap : ClassMap<Filter> {
        public FilterMap() {
            Table("Filters");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Description);
            References(x => x.Search).Column("SearchId");
        }
    }
 public class RoleMap : ClassMap<Role> {
        public RoleMap() {
            Table("Roles");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Name);
            References(x => x.DefaultSearch).Column("DefaultSearchId");
        }
    }

Code:

class Program {
        static void Main() {
            var sessionFactory = CreateSessionFactory();
            using (var session = sessionFactory.OpenSession()) {
                var search = session.Get<Search>(1);
                foreach (var filter in search.Filters) {
                    Console.WriteLine(filter);
                }
            }
        }

        static ISessionFactory CreateSessionFactory(){
            string connectionString = @"server=.\sql2008; user id = sa; pwd=1; database = nhbug;";
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
                .Mappings(m=>m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())).BuildSessionFactory();
        }
    }

ERROR:

When accessing the search.Filters property, NHibernate tries to access Filters.DefaultSearchId db column which is not supposed to be there. This column exists in Roles table but not in filters.

QUESTION:

Is it invalid configuration, Fluent NHibernate or NHibernate bug?

I’m using SQL Server 2008 R2, NHibernate 2.1.2 and Fluent NHibernate 1.1.0.685, although this issue exists in NHibernate 3 beta 2 as well.

Thank you.

UPDATE:
Here is the actual SQL generated

UPDATE2: CDMDOTNET, same error, same sql, unfortunately.

UPDATE3: Actual exception

UPDATE4: This is a particular use case of a general bug: Entity references other entities as ‘many-to-many’ and on the other side of ‘many-to-many’ assoc. the other entity references the source entity (DefaultQuery in my case). NH goes nuts when accessing any child collection (one-to-many) of a source entity (Filters in my case).

UPDATE5: Sample data

UPDATE6: XML issued by Fluent NHibernate

  • 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-18T07:25:20+00:00Added an answer on May 18, 2026 at 7:25 am

    Update the HasMany mapping on the SearchMap to include the KeyColumn():

    HasMany(x => x.Filters).KeyColumn(“SearchId”).Inverse().Cascade.All().AsBag();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: There are a bunch of .lnk files on the C drive that point
I am trying to find an optimal solution for the following problem: there is
Is there a problem with using IEnumerable<T> as a return type? FxCop complains about
Problem, there's no method: bool ChangePassword(string newPassword); You have to know the current password
So there seems to be this problem with GNU Make's $(wildcard) function keeping a
While there are 100 ways to solve the conversion problem, I am focusing on
is there an easy way to solve the following problem. Let's say I fetch
There is very strange assembly reference problem and loading problem I experience with my
There are many skills a programmer could have (understanding the problem, asking good questions,

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.