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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:55:16+00:00 2026-05-26T04:55:16+00:00

I have a mapping issue with the table-per-class hierarchy in Fluent/NHibernate. When retrieving the

  • 0

I have a mapping issue with the table-per-class hierarchy in Fluent/NHibernate. When retrieving the records from the database, I keep getting an error (the Wrong type exception)

Object with id: 2445763 was not of the specified subclass: ClassA (loading object was of wrong class [ClassB]) (record 2445763 does have the value “2” in the Type column)

In my domain, I have EntryBase, ClassA and ClassB. The classes are defined as

public abstract class EntryBase 
{
    public virtual int Id {get;set;}
    public virtual string CommonProperty1 {get;set;}
    *... (lots of other common properties)*
    public virtual string CommonPropertyN {get;set;}
}

public class ClassA : EntryBase 
{
    public virutal string ClassAOnlyProperty {get;set;}
}

public class ClassB : EntryBase 
{
    public virutal string ClassBOnlyProperty {get;set;}
    public virutal int ClassBOnlyOtherProperty {get;set;}
}

The mappings are:

public class EntryBaseMap : ClassMap<EntryBase>
{
    public EntryBaseMap()
    {
        Table("MySingleTable");
        Id(x => x.Id, "RecordId").GeneratedBy.Identity();
        Map(x => x.CommonProperty1, "Field1Name");
        ...
        Map(x => x.CommonPropertyN, "FieldNName");
        DiscriminateSubClassesOnColumn<string>("Type");
    }
}

public class ClassAMap : SubclassMap<ClassA>
{   
    public ClassAMap()
    {
        DiscriminatorValue("1");
        Map(x => x.ClassAOnlyProperty);
    }
}

public class ClassBMap : SubclassMap<ClassB>
{   
    public ClassBMap()
    {
        DiscriminatorValue("2");
        Map(x => x.ClassBOnlyProperty);
        Map(x => x.ClassBOnlyOtherProperty);
    }
}

Any idea what might be amiss? I’ve correctly been able to store records of Class B, but when I retrieve them, it’s attempting to load them as Class A. Is it a mapping problem?

  • 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-26T04:55:16+00:00Added an answer on May 26, 2026 at 4:55 am

    As discussed in the comments, if you have collection properties meant to represent a single subclass, you need to add a where clause in the mapping:

    .Where("Type = '1'")
    

    Type is your discriminator column and 1 is the discriminator value that matches the type you’re trying to load.

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

Sidebar

Related Questions

I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I am having an issue with many-to-many mapping using NHibernate. Basically I have 2
I am trying to merge records from an Oracle database table to my local
I have the following code in my context, and no explicit table-class mapping, yet
I have what I think should be a fairly simple mapping issue, but not
In C# I have a use case where I have a mapping from int
I have a class ClassA mapped to TableA. I also have a mapping for
I'm way out of my league here... I have a mapping table (table1) to
I have a relation mapping table like this: attributeid bigint productid bigint To clean
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet

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.