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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:44:56+00:00 2026-05-24T18:44:56+00:00

I made conversion from EF4 to nHibernate and now have little problem with inheritence.

  • 0

I made conversion from EF4 to nHibernate and now have little problem with inheritence.

My entities and mappings:

public class User
{
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
}
public class Account
{
    public virtual int Id { get; set; }
    public virtual User User { get; set; }
}
public class Member : User
{
    public virtual string SpecialPropForMember { get; set; }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.UserName);
    }
}
public class AccountMap : ClassMap<Account>
{
    public AccountMap()
    {
        Id(x => x.Id);
        References(x => x.User);
    }
}
public class MemberMap : SubclassMap<Member>
{
    public MemberMap()
    {
        Map(x => x.SpecialPropForMember);
    }
}

My passed test:

    [Test]
    public void TestMemberUserInheritence()
    {
        User newUser = new User()
        {
            UserName = RandomValues.String()
        };

        Member newMember = new Member()
        {
            SpecialPropForMember = "special"
        };


        Account newAccount = new Account()
        {
            User = newMember
        };

        Member member = account.User as Member;
        Assert.IsNotNull(member);
    }

and failed test:

    [Test]
    public void TestMemberUserInheritenceFromNHibernate()
    {
        User newUser = new User()
        {
            UserName = RandomValues.String()
        };
        UsersService().AddUser(newUser);

        Member newMember = new Member()
        {
            SpecialPropForMember = "special"
        };
        MemberService().Add(newMember);

        Account newAccount = new Account()
        {
            User = newMember
        };
        AccountService().Add(newAccount);

        Account account; ;
        using (var session = DataAccess.OpenSession())
        {
            account = session.Linq<Account>().First();
        }

        Member member = account.User as Member;
        Assert.IsNotNull(member);
    }

Could someone explain me why NH don’t resolve properly inheritance ?
The same issue is for table per class and table per hierarchy.

  • 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-24T18:44:57+00:00Added an answer on May 24, 2026 at 6:44 pm

    It’s because of the way that NHibernate provides lazy loading. Value of account.User in your failing test is actually a proxy object. This means, that when you’re loading account, the user is not fetched from DB until you access any of it’s properties (unless you state so explicitly in your query). This means, that when NHibernate creates the proxy object, it does not know what is actual type of this object, and it creates proxy object that derives directly from User class. I’ve gone into more details in my anwser to this question: Force lazy entity to load real instance.

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

Sidebar

Related Questions

Seriously I'm confused. I have an .XSD that is made from a class so
I have a doubt, sometime I made this conversion from DataTable to List<T> :
Suppose I have a class: class MyClass { public int MyMethod(Func<int, int> f) {
Well in my program i have made a class of name 'Transition'. When i
I just encountered this (made up code to demonstrate the problem): public ICollection<string> CreateCollection(int
Made this nice little loop for hiding and showing div's, works as a charm
I made an iphone app to capture image from camera and to set that
I made a Web service in which I have a function to count some
I made a little PHP script that checks if an email is valid. The
I made a UITableViewCell and I have 20 rows in that table in which

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.