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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:09:58+00:00 2026-05-10T21:09:58+00:00

Suppose I have three classes. It is valid to instantiate A, but there are

  • 0

Suppose I have three classes. It is valid to instantiate A, but there are also special cases B and D which subclass A, adding additional information.

How would I do the mapping files for this in (fluent) NHibernate?

public class A {     public int ID { get; set;}     public string CommonProperty1 { get; set; }     public string CommonProperty2 { get; set; } }  public class B : A {     public string BSpecificProperty1 { get; set; } //not null     public string BSpecificProperty2 { get; set; } //not null }  public class D : A {     public string DSpecificProperty { get; set; } //not null } 

I tried the following, but it doesn’t work at all:

public class AMap : ClassMap<A> {     public AMap()     {         Id(x => x.ID);          Map(x => x.CommonProperty1);         Map(x => x.CommonProperty2);     } }  public class BMap : ClassMap<B> {     public BMap()     {         References(x => x.ID);         Map(x => x.BSpecificProperty1)             .CanNotBeNull();         Map(x => x.BSpecificProperty2)             .CanNotBeNull();     } }  public class DMap : ClassMap<D> {     public DMap()     {         References(x => x.ID);          Map(x => x.DSpecificProperty)             .CanNotBeNull();     } } 
  • 1 1 Answer
  • 2 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. 2026-05-10T21:09:58+00:00Added an answer on May 10, 2026 at 9:09 pm

    I’m not sure I understand what you mean by ‘map a subclass one-to-one’, but if you want to map inheritance where the subclasses have properties that are not nullable, you can do like this in Fluent-NHibernate:

    // Domain classes public class Animal {     public virtual int Id { get; set; }     public virtual string Name { get; set; } }  public class Cat : Animal {     public virtual int WhiskerLength { get; set; }     public virtual int ClawCount { get; set; } }  public class Dog : Animal {     public virtual int TailWagRate { get; set; } }    // Mapping file public class AnimalMap : ClassMap<Animal> {     public AnimalMap()     {         Id(x => x.Id)             .WithUnsavedValue(0)             .GeneratedBy.Native();          Map(x => x.Name);          var catMap = JoinedSubClass<Cat>('CatId', sm => sm.Map(x => x.Id));          catMap.Map(x => x.WhiskerLength)             .CanNotBeNull();         catMap.Map(x => x.ClawCount)             .CanNotBeNull();          JoinedSubClass<Dog>('DogId', sm => sm.Map(x => x.Id))             .Map(x => x.TailWagRate)                 .CanNotBeNull();     } } 

    Since you want the subclasses’ properties to be not-null, you have to use the table-per-class (joined-subclass) way of modeling the inheritance. This is because table-per-hierarchy requires all subclass properties to be nullable.

    I hope it helps.

    /Erik

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

Sidebar

Related Questions

Suppose I have a List<IMyInterface> ... I have three classes which implement IMyInterface :
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
Suppose I have three classes A,B and C generated via emit/reflection abilities of .NET
Suppose I have three models: Student , SchoolClass , and DayOfWeek . There is
Suppose I have the following three classes: class Animal {}; class Human : public
I have three classes: a TopClass which contains an instance of a BottomClass pointer.
The problem Suppose I have three classes A, B and C, where B is
Suppose I have a three level hierarchy consisting of school, students, and classes. If
Suppose I have three projects in my sln. (1) xyz.a{Class Lib}{no reference added} (2)
Suppose I have three lists: list1 = a, c, d, r, t list2 =

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.