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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:34:21+00:00 2026-05-19T05:34:21+00:00

Given these classes: using System.Collections.Generic; namespace FluentMappingsQuestion { public class Entity { public virtual

  • 0

Given these classes:

using System.Collections.Generic;

namespace FluentMappingsQuestion
{
    public class Entity
    {
        public virtual int Id { get; set; }
        public virtual IDictionary<string, Property> Properties { get; set; }
    }

    public class Property
    {
        public virtual Entity OwningEntity { get; set; }
        public virtual string Name { get; set; }
        public virtual int Value { get; set; }
        public virtual decimal OtherValue { get; set; }
    }
}

How can I map them using NHibernate (preferably fluent flavor) so that doing this is possible:

[Test]
public void EntityPropertyMappingTest()
{
    using (var session = _factory.OpenSession())
    {
        var entity = new Entity();

        // (#1) I would *really* like to use this
        entity.Properties["foo"] = new Property { Value = 42, OtherValue = 42.0M };

        session.Save(entity);
        session.Flush();

        // (#2) I would like the entity below to "update itself"
        // on .Save or .Flush. I got it to work with .Load()
        Assert.AreEqual(42, entity.Properties["foo"].Value);
        Assert.AreEqual(42.0M, entity.Properties["foo"].OtherValue);
        Assert.AreEqual("foo", entity.Properties["foo"].Name);
        Assert.AreEqual(entity, entity.Properties["foo"].Owner);
    }
}

I have almost managed to do this with these mappings:

// class EntityMap : ClassMap<Entity>
public EntityMap()
{
    Id(x => x.Id);
    HasMany(x => x.Properties)
    .Cascade.AllDeleteOrphan()
    .KeyColumn("EntityId")
    .AsMap(x => x.Name);
}

// class PropertyMap : ClassMap<Property>
public PropertyMap()
{
    Id(x => x.Id);
    References(x => x.OwningEntity).Column("EntityId");
    Map(x => x.Name).Length(32);
    Map(x => x.Value);
{

The problems I have:

  • If I make Entity.Properties .Inverse(), it starts breaking
  • If I don’t make it .Inverse() then NHibernate does: INSERT(Entity), INSERT(Property), UPDATE(Property) instead of just INSERT(Entity), INSERT(Property)
  • If I make Property.Name .Not.Nullable(), it starts breaking
  • If I don’t make it .Not.Nullable(), I have a hole in my db schema

How should I change my mappings?

  • 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-19T05:34:22+00:00Added an answer on May 19, 2026 at 5:34 am

    I worked around this by specifying this mapping:

    HasMany<Property>(Reveal.Member<Entity>("InternalProperties"))
      .AsMap(p => p.Name)
      .Cascade.AllDeleteOrphan()
      .Inverse();
    

    and creating two properties of type IDictionary<string, Property>: Properties and InternalProperties. The first one is a proxy dictionary over the second one, and deals with setting the OwningEntity and Name properties for the Property entries.

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

Sidebar

Related Questions

Given this declaration: using System; using System.Collections; using System.Collections.Generic; namespace AProject.Helpers { public static
What are the potential pros and cons of each of these queries given different
Is there any simple way to generate a default crud (given an entity) with
I'm eager to give these a go, but I've only got my precious home
I have a table that contains tasks and I want to give these an
apparently it works Can you name reasons beyond good practices not to give these
Given that there are many sizes and color depths for different phones (even for
This is not a technical question, but given that there are a few iPhone
What are the options? How popular are they? Do these IDEs give similar/better functionality
given a matrix of distances between points is there an algorithm for determining a

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.