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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:38:44+00:00 2026-05-22T19:38:44+00:00

How do I map the collection of Parts using a convention? public class Part

  • 0

How do I map the collection of Parts using a convention?

public class Part
{
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
}

public class Car
{
  private readonly List<Part> _parts = new List<Part>();

  public virtual int Id { get; set; }

  public virtual IList<Part> Parts
  {
    get { return _parts.AsReadOnly(); }
  }
}

I have tried this convention but it always expects the field name without an underscore prefix:

public class HasManyConvention : IHasManyConvention
{
  public void Apply(IOneToManyCollectionInstance instance)
  {
    instance.Access.ReadOnlyPropertyThroughCamelCaseField(CamelCasePrefix.Underscore);
  }
}

I’ve tried it with the 1.2.0.694 and 2.0.0.698 builds with the same result:

"Could not find field 'parts' in class 'TestFluentNHibernate.Car'"
  • 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-22T19:38:45+00:00Added an answer on May 22, 2026 at 7:38 pm

    First of all, your _parts member can’t be read-only. NHibernate needs write access to the member to set the value/reference. To return a truly read-only collection through the Parts property you have to return a System.Collections.ObjectModel.ReadOnlyCollection. This also “removes” all the methods found in the read/write collection types that are still there if you just return f.ex. list.AsReadOnly(). A read-only list returned in this way still have .Add() method and others to edit the collection, but they will cause a runtime exception so returning a ReadOnlyCollection to begin with is a great for preventing this possibility.

    A lot of people seem to like returning an IEnumerable which is also read-only, but it can be cast to a List or other read/write collection type and changed that way.

    You have to implement AddPart and RemovePart methods to allow external code to add and remove items to the read-only collection.

    Your convention looks correct, I’m using the exact same syntax with 1.2.0.694 successfully, my convention:

         instance.Key.Column(instance.EntityType.Name + "Fk");
         instance.Fetch.Subselect();
         instance.Inverse();
         instance.Cascade.All();
         instance.Access.ReadOnlyPropertyThroughCamelCaseField(CamelCasePrefix.Underscore);
    

    Example class:

    public class Car
    {
        private List<Part> _parts;
    
        public Car()
        {
            // Initialize member collection _parts
            _parts = new List<Part>();
        }
    
        public virtual int Id { get; set; }
    
        // Return readonlycollection here, ReadOnlyCollection implements 
        // IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable,
        // so the returned list can be any collection type inheriting from these base types.
        public ReadOnlyCollection<Part> Parts
        {
            get { return new List<Part>(_parts).AsReadOnly(); } }
        }
    
        public AddPart(Part part)
        {
            // Usually I don't want duplicates
            if (!_parts.Contains(part))
                _parts.Add(part);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using annotations to map a basic collection of Strings to an existing
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
How can I map the value of an object collection using fluent mapping? I
Let's assume we have the following Map index: public class CommentsIndex : AbstractIndexCreationTask<Post> {
I'm trying to map a collection (of type map) using a foreign key and
I define a collection that is supposed to map two parts of a line
iwant to map collection of model to viewmodel with automapper this is my code
I have a collection of the form: map<key, list<object> > I only ever insert
I was surprised by the fact that Map<?,?> is not a Collection<?> . I
why do i get this exception? Map myHash = null myHash = (HashMap)Collections.synchronizedMap(new HashMap());

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.