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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:52:09+00:00 2026-05-26T10:52:09+00:00

I’m trying to learn NH (ver. 3.2) and am having problems understanding some of

  • 0

I’m trying to learn NH (ver. 3.2) and am having problems understanding some of the mapping choices. Scott Findlater has posted his fully working skeleton for sexy Loquacious NH here and my questions about mapping(s) will be based in his sample.
Here is the domain model (pic not part of his sample but included here for clarity):

enter image description here

His category class looks like this:

public class Category : Entity
    {
        public Category()
        {
            Products = new List<Product>();
            SubCategories = new List<Category>();
        }

        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual Category Parent { get; set; }
        public virtual IEnumerable<Category> SubCategories { get; set; }
        public virtual IList<Product> Products { get; set; }
    }

and his mapping class like this:

class CategoryMap : ClassMapping<Category>
    {
        public CategoryMap()
        {
            // **************************************************
            // Mapping of Id here will take precedence over the 
            // global conventions configured in the ModelMapper.
            // **************************************************
            //Id(x => x.Id, map =>
            //{
            //    map.Column("Id");
            //    map.Generator(Generators.GuidComb);
            //});

            Property(x => x.Name, m => m.Length(450));
            Property(x => x.Description, m => m.Length(2000));

            Set(x => x.SubCategories, set =>
                                          {
                                              set.Key(k => k.Column("ParentCategoryId"));
                                              set.Inverse(true);
                                          } ,
                                       ce => ce.OneToMany());

            ManyToOne(x => x.Parent, manyToOne =>
                                         {
                                             manyToOne.Column("ParentCategoryId");
                                             manyToOne.Lazy(LazyRelation.NoLazy);
                                             manyToOne.NotNullable(false);
                                         });


            Set(x => x.Products, set =>
                                    {
                                        set.Key(key =>
                                        {
                                            key.Column("ProductId");
                                            key.ForeignKey("FK_Product_Category_ProductId");
                                        });
                                        set.Table("Product_Category");
                                    },
                                    ce => ce.ManyToMany(m => m.Column("CategoryId")));
        }
    }

Now, for the questions:

1) Why did he chose to map/model Parent property as ManyToOne relationship? Doesn’t this suggest that Category can belong to more than one parent category, i.e. any given category (other than the root I suppose) can be spread out among many other parent categories? If so then this is not very clear from the model itself because to me the Parent (as I see it in the class definition) looks like a property of type Category and I’d map it like that. When would you chose approach in this solution v.s. mapping it as a simple property?

2) When I map files, who’s (or which) perspective I should be looking from? Seems to me like this is from the inside of the class you are trying to map. So, in this case, when I’m trying map the Category class, I’m only concerned with mapping the “arrows” going out, right?

3) Person creating mapping files must have knowledge outside of what’s clear just by looking at a class. Look at how Products property is mapped (ManyToMany relationship). From the class itself it’s only clear that Category may belong to many Products but Category doesn’t know that it may be contained in many Products. That fact is obvious for the Product class. Now that I’ve said that seems to me that when I create mapping file(s) I should be looking from the point of view of a class AND then a model.

4) Generally speaking: when do you use .Inverse(), .Cascade() and .Lazy()?

5) What are the differences between mapping using ModelMapper vs ConventionModelMapper? I haven’t studied files included in the same project which use the latter method but I wanted to know if there is, other than a preference, an advantage in using one over another.

  • 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-26T10:52:10+00:00Added an answer on May 26, 2026 at 10:52 am
    1. Scott decided to establish an Ordinary Association(many-to-one) association between a Category and its Parent(another) Category. A many-to-one association gives you access to features such as Cascading and Fetch strategies that you will not find in a property mapping. For example, imagine you want to delete a parent and all its children. Cascades can help you out.

    2. If you have an association, you must think about both sides of the association. For example, say you have a bidirectional one-to-many association between ‘A’ and ‘B’, you need to pay attention as to which side manages the relationship. When you are writing a mapping for ‘A’, you think in terms of how ‘A’ relates to be, and viceversa when mapping ‘B’.

    3. Your point is a bit unclear to me. However, what you seem to be describing is basic OOP object relationships. A class may have property ‘A’ that IS a relationship to object ‘B’ and ‘B’ in turn may relate to ‘C’. Just by looking at ‘A’ it is impossible to tell that it is transitively related to ‘C’. There is nothing wrong with that.

    4. Read the documentation.

      4.1 Inverse: Defines which side of a bidirectional relationship manages the relationship.

      4.2 Cascade: Allows certain operations to cascade to children associations. (e.g. Deletes)

      4.3 Lazy: Defines the strategy to load collection. (Eagerly/Lazily)

    5. The ConventionalModelMapper is built on top of the ModelMapper and provides convenience methods and default mapping conventions that make the mapping-by-conventions experience easier.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.