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

  • Home
  • SEARCH
  • 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 213931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:19:33+00:00 2026-05-11T18:19:33+00:00

Given a Vehicle class and a VehicleProperty class… public class Vehicle { public virtual

  • 0

Given a Vehicle class and a VehicleProperty class…

public class Vehicle
{
    public virtual int Id { get; protected set; }
    public virtual string Registration { get; set; }

    private List<VehicleProperty> _properties = new List<VehicleProperty>();
    public virtual IEnumerable<VehicleProperty> Properties
    {
        get { return _properties; }
        protected set{ _properties = new List<VehicleProperty>(value);}
    }

    public virtual void AddProperty(string name, string value)
    {
        _properties.Add(new VehicleProperty {Name = name, Value = value});
    }
}

public class VehicleProperty
{
    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
}

How can I map the two classes so that the VehicleProperty table has a composite key of [VehicleId] and [Name]. Vehicle would be an aggregate root (VehicleProperty is not accessed outside of a Vehicle class).

I have tried everything that I can think of (I’m new to NHibernate so that’s not much)

public class VehicleMap : ClassMap<Vehicle>
{
    public VehicleMap()
    {
        Id(x => x.Id);
        Map(x => x.Registration);
        HasMany(x => x.Properties)
            .Inverse()
            .Cascade.All();
    }
}

public class VehiclePropertyMap : ClassMap<VehicleProperty>
{
    public VehiclePropertyMap()
    {
        UseCompositeId()
            .WithKeyProperty(x => x.Name)
            .WithKeyReference(x => x.Vehicle, "Vehicle_Id");
        Map(x => x.Name);
        Map(x => x.Value);
    }
}

This mapping results in the below sql and a StaleStateException “Unexpected row count: 0; expected: 1” (I also don’t really want to have a Vehicle property on the VehicleProperty)…

INSERT INTO "Vehicle" (Registration) VALUES (@p0); select last_insert_rowid(); @p0 = 'AA09CDE'
UPDATE "VehicleProperty" SET Name = @p0, Value = @p1 WHERE Name = @p2 AND Vehicle_Id = @p3; @p0 = 'Colour', @p1 = 'Black', @p2 = 'Colour', @p3 = ''
  • 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-11T18:19:33+00:00Added an answer on May 11, 2026 at 6:19 pm
    1. Don’t use composite id’s.
    2. Inverse means, that this is the
      inverse relation of an other one. If
      there is no other one, it’s just not
      updated.
    3. You declared the VehicleProperty.Name Property
      as the primary key. if the primary
      key is already initialized, NH
      thinks it is already stored and
      therefore tries an update. (that’s why you get the exception.) You can
      changed this behaviour, but it’s
      better to use an artificial primary
      key or the mapping bellow.

    I don’t know FluentNHibernate to show you the code. I can tell you how it looks in XML.

    <class name="Vehicle">
      <id name="Id" generator="native"/>
    
      <property name="Registration"/>
    
      <!-- declare the table Vehicle_Properties for the property Properties -->
      <bag name="Properties" table="Vehicle_Properties" cascade="all-delete-orphan">
    
        <!-- primary key of Vehicle_Properties and foreign key -->
        <key column="Vehicle_FK"/>
    
        <!-- contents of the table: Name and Value -->
        <composite-element class="VehicleProperty">
          <property name="Name"/>
          <property name="Value"/>
        </composite-element>
      </bag>
    
    </class>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the existing buttons HTML: <div id=MB> <ul class=list> <li id=post-5><a href=#post-5>5</a></li> <li id=post-4><a
Given a Python object of any kind, is there an easy way to get
Given an interface or interfaces, what is the best way to generate an class
Given an Entity Data Model (EDMX) with Code Generation Strategy set to None, how
Given: <div class=subMenu> <a href=products.aspx class=subLink>Products</a> <a href=productCats.aspx class=subLink>Categories</a> <a href=example.aspx class=subLink>Another</a> </div> In
Given a public SOAP web service and no WSDL, I need to build a
Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a DateTime representing a person's birthday, how do I calculate their age in

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.