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
  • 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. 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

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Figured this out. Prerequisites: Windows API Code Pack static void… May 13, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer If you are talking about the Forms designer you might… May 13, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer It turns out this is possible after all. My problem… May 13, 2026 at 8:44 am

Related Questions

Suppose I have a Monkey class which sometimes needs to acquire an instance of
Suppose I have a class 'Application'. In order to be initialised it takes certain
I have been looking at various dependency injection frameworks for .NET as I feel
Currently I am working on the design of my degree project. Some days ago

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.