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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:58:04+00:00 2026-06-14T04:58:04+00:00

I have defined this mapping: public class Mapping : ConventionModelMapper { public Mapping() {

  • 0

I have defined this mapping:

public class Mapping : ConventionModelMapper
{
    public Mapping()
    {
        IsRootEntity((type, declared) =>
                         {
                             return !type.IsAbstract &&
                                    new[] { typeof(Entity<Guid>), typeof(CommonEntity) }.Contains(type.BaseType);
                         });

        IsEntity((x, y) => typeof(Entity<Guid>).IsAssignableFrom(x) && !x.IsAbstract && !x.IsInterface);

        IsSet((mi, wasDeclared) =>
                  {
                      var propertyType = mi.GetPropertyOrFieldType();
                      return propertyType.IsGenericType && typeof(System.Collections.Generic.ISet<>).IsAssignableFrom(propertyType.GetGenericTypeDefinition());
                  });

        IsManyToMany((mi, wasDeclared) =>
                         {
                             var propertyType = mi.GetPropertyOrFieldType();
                             var containingType = mi.ReflectedType;

                             if (typeof(System.Collections.Generic.ISet<>).IsAssignableFrom(propertyType.GetGenericTypeDefinition()))
                             {
                                 var referenceType = propertyType.GetGenericArguments()[0];
                                 return true;
                                 return !referenceType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Any(p => p.PropertyType.IsAssignableFrom(containingType));
                             }
                             return false;
                         });

        Class<Entity<Guid>>(x =>
                                {
                                    x.Id(c => c.Id, m => m.Generator(Generators.GuidComb));
                                    x.Version(c => c.Version, (vm) => { });
                                });

        BeforeMapClass += OnBeforeMapClass;
        BeforeMapManyToOne += OnBeforeMapManyToOne;
        BeforeMapSet += OnBeforeMapSet;
        BeforeMapManyToMany += OnBeforeMapManyToMany;

        Class<CommonEntity>(x =>
        {
            x.Property(c => c.DateCreated, m => m.Type<UtcDateTimeType>());
            x.Property(c => c.DateModified, m => m.Type<UtcDateTimeType>());
        });
    }

    private void OnBeforeMapManyToMany(IModelInspector modelInspector, PropertyPath member, IManyToManyMapper collectionRelationManyToManyCustomizer)
    {
        collectionRelationManyToManyCustomizer.Column(member.LocalMember.GetPropertyOrFieldType().GetGenericArguments()[0].Name + "Id");
    }

    private void OnBeforeMapSet(IModelInspector modelInspector, PropertyPath member, ISetPropertiesMapper propertyCustomizer)
    {
        propertyCustomizer.Key(k=>k.Column(member.GetContainerEntity(modelInspector).Name + "Id"));
        propertyCustomizer.Cascade(Cascade.Persist);
        if (modelInspector.IsManyToMany(member.LocalMember))
        {
            propertyCustomizer.Table(member.GetContainerEntity(modelInspector).Name +
                                     member.LocalMember.GetPropertyOrFieldType().GetGenericArguments()[0].Name);
        }
    }

    private void OnBeforeMapManyToOne(IModelInspector modelInspector, PropertyPath member, IManyToOneMapper propertyCustomizer)
    {
        propertyCustomizer.Column(member.LocalMember.Name + "Id");
    }

    private void OnBeforeMapClass(IModelInspector modelInspector, Type type, IClassAttributesMapper classCustomizer)
    {
        classCustomizer.Table('['+ type.Name + ']');
    }
}

An I am having a problem with a many to many relationship. I have User, UserPermission and Permission. When I am saving a user after attaching a Permission to it it generates this SQL:

exec sp_executesql N'UPDATE [Permission] SET UserId = @p0 WHERE Id = @p1',N'@p0 uniqueidentifier,@p1 uniqueidentifier',@p0='57A2CD87-4A79-4131-B9CE-A1060168D520',@p1='9D99D340-1B63-4291-B55A-6127A8F34FC9'

When it should be like:

exec sp_executesql N'INSERT INTO UserPermission (UserId, PermissionId) VALUES (@p0, @p1)',N'@p0 uniqueidentifier,@p1 uniqueidentifier',@p0='2C670A01-C2E6-46A3-A412-A1060168F976',@p1='9D99D340-1B63-4291-B55A-6127A8F34FC9'

When I add a specific class mapping for User:

        Class<User>(classMapper =>
                        {
                            classMapper.Set(x => x.Permissions, map =>
                                                        {
                                                            //map.Key(k => k.Column("UserId"));
                                                            //map.Table("UserPermission");
                                                        }, r => r.ManyToMany(m => {}));
                        });

I can leave out the key and table definition and include the ManyToMany without the column call and it works. But it does the same same thing as my BeforeManyToMany event handler. If I drop the whole Class thing the BeforeMapManyToMany event is not fired and nHibernate thinks I’ve got a UserId on my Permission table.

Heres User:

public class User : CommonEntity
{
    protected User()
    {
        Permissions = new HashSet<Permission>();
    }

    public User(User createdBy) : base(createdBy)
    {
        Permissions = new HashSet<Permission>();
    }

    public ISet<Permission> Permissions { get; protected set; }
}
  • 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-14T04:58:05+00:00Added an answer on June 14, 2026 at 4:58 am

    After poking around in the source code I realised the problem was that IsOneToMany was checked against the property when defining the set before IsManyToMany. I just needed to define IsOneToMany and it worked without any explicit mappings.

            IsOneToMany((mi, wasDeclared) =>
            {
                var propertyType = mi.GetPropertyOrFieldType();
    
                return ModelInspector.IsEntity(propertyType);
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree
I have an abstract entity base class defined like this: public abstract class SessionItem
I have this mapping defined in my Application Layer: public IList<ProfessionDTO> GetAllProfessions() { IList<Profession>
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have defined my Enums like this. public enum UserType { RESELLER(Reseller), SERVICE_MANAGER(Manager), HOST(Host);
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have the following Java class defined in src/java package org.davisworld.trip; public class AirportHbm
I have the following model defined in .NET: Public Class Request <Key()> Public Property
I have a service which has one endpoint , I have defined this endpoint
I have defined a format like this: # header format format STDOUT_TOP = pid

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.