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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:41:46+00:00 2026-06-03T13:41:46+00:00

I try to query data using FluentNhibernate and I get this error: Sequence contains

  • 0

I try to query data using FluentNhibernate and I get this error: “Sequence contains more than one matching element”

Here are my classes and mappings:

public class Course
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual IList<Instructor> Instructors { get; set; }
}

public class Instructor
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual ImageData Portrait { get; set; }
    public virtual ImageData PortraitThumb { get; set; }
    public virtual IList<Course> TeachingCourses { get; private set; }
}

public class ImageData : Entity
{
    public virtual int Id { get; private set; }
    public virtual byte[] Data { get; set; }
}

public class CourseMap : ClassMap<Course>
{
    public CourseMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasManyToMany(x => x.Instructors)
            .Cascade.All()
            .Table("CourseInstructor");
    }
}

public class InstructorMap : ClassMap<Instructor>
{
    public InstructorMap()
    {
        Id(x => x.Id);
        Map(x=> x.Name);
        References(x => x.Portrait)
            .Nullable()
            .Cascade.All();
        References(x => x.PortraitThumb)
            .Nullable()
            .Cascade.All();
        HasManyToMany(x => x.TeachingCourses)
            .Cascade.All()
            .Inverse()
            .Table("CourseInstructor");
    }
}

public class ImageDataMap : ClassMap<ImageData>
{
    public ImageDataMap()
    {
        Id(x => x.Id);
        Map(x => x.Data);
    }
}

Then I try to get data using below code:

var course = session.CreateCriteria(typeof(Course))
               .SetFetchMode("Instructors", FetchMode.Eager)
               .SetFetchMode("Instructors.Portrait", FetchMode.Eager)
               .SetFetchMode("Instructors.PortraitThumb", FetchMode.Eager)
               .List<Course>();

But I get the following error: “Sequence contains more than one matching element”
Also, when I try this

var course = session.CreateCriteria(typeof(Course))
               .SetFetchMode("Instructors", FetchMode.Eager)
               .SetFetchMode("Instructors.Portrait", FetchMode.Eager)
               .SetFetchMode("Instructors.PortraitThumb", FetchMode.Eager)                   
               .SetResultTransformer(new DistinctRootEntityResultTransformer())
               .List<Course>();

No error occurs but I get duplicate Instructor objects.

I did try below posts and some others as well. But it doesn’t help.

  • NHibernate Eager loading multi-level child objects
  • Eager Loading Using Fluent NHibernate/Nhibernate & Automapping
  • 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-06-03T13:41:47+00:00Added an answer on June 3, 2026 at 1:41 pm

    FluentNhibernate uses a bag-mapping for many-to-many relations, if the mapped property is of type IList.

    A bag mapping has a few major drawbacks Performance of Collections / hibernate. The one that currently bites you is that NH does not permit duplicate element values and, as they have no index column, no primary key can be defined.

    Simply said NH does not know to which bag do they belong to when you join them all together.

    Instead of a bag I would use a indexed variant a set, assuming that an Instructor does not has the same persistent Course assigned twice.

    You can fix your query results by amending your domain classes, this tells FluentNhibernate to use a set instead of a bag by convention:

    public class Course
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual Iesi.Collections.Generic.ISet<Instructor> Instructors { get; set; }
    }
    
    public class Instructor
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual ImageData Portrait { get; set; }
        public virtual ImageData PortraitThumb { get; set; }
        public virtual Iesi.Collections.Generic.ISet<Course> TeachingCourses { get; private set; }
    }
    

    In addition you can amend your mapping by using .AsSet(). FluentNHibernate: What is the effect of AsSet()?

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

Sidebar

Related Questions

I am trying to display the data using parametrized query try { SqlConnection xconn
I'm using the follow query to try and identify data from a table with
I'm using SQL Server 2008 with Report Builder 2.0 to try and query data
I'm using the following HQL query to try and load a set of objects
If I try to run this query in SQL Server 2005: SELECT 1 WHERE
When I try to run any update query in Access 2007, I get the
Query's throwing an ORA-00907 Error when I try to paste a list of values
I imported a table from a CSV file into MySQL using this query: LOAD
I was trying to to use parameterized query with data reader when I get
I'm trying to write a program that cleans data, using Matlab. This program takes

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.