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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:36:10+00:00 2026-05-14T03:36:10+00:00

I think I probably know that the most common suggestion will be change the

  • 0

I think I probably know that the most common suggestion will be “change the database schema” but unfortunately it’s one of those “I’m stuck with a legacy database” situations.

Basically I want to map the following using Fluent NHibernate

CREATE TABLE Person (PersonId int)
CREATE TABLE Organisation (OrganisationId int)
CREATE TABLE OwnedPhoneNumber (Id int, PersonId int, OrganisationId int, 
PhoneNumber varchar(50))

interface IPhoneNumberOwner
{
    int Id {get; set;}
}
class Person : IPhoneNumberOwner
{
  public virtual int Id {get; set;}
  public virtual ISet<OwnedPhoneNumber> PhoneNumbers {get; set;}
}
class Organisation : IPhoneNumberOwner
{
  public virtual int Id {get; set;}
  public virtual ISet<OwnedPhoneNumber> PhoneNumbers {get; set;}
}
class OwnedPhoneNumber
{
  public virtual int Id {get; set;}
  public virtual IPhoneNumberOwner Owner {get; set;}
  public virtual string PhoneNumber {get; set;}
}

My question is how do I create the ClassMap file for the OwnedPhoneNumber class to determine the column to use when persisting an entity i.e. if it’s a Person object use the PersonId column and if it’s an Organisation use the OrganisationId column. Mapping the collections from the parent classes is no problem.

Hopefully I’ve included enough detail to explain the problem but obviously shout if not.

  • 1 1 Answer
  • 1 View
  • 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-14T03:36:11+00:00Added an answer on May 14, 2026 at 3:36 am

    Noting this in case anyone comes across the same issue but I’ve found a workaround that some may like (and some may not). After some less than helpful answers I’ve come to the conclusion that (leaving the database schema discussions aside) this can’t be done with nhibernate. I therefore decided to create two views in the database and present these as tables to nhibernate in the mapping. i.e.

    CREATE VIEW dbo.vw_PersonPhoneNumber
    AS
    SELECT  pn.Id,
        pn.PersonId,
        pn.PhoneNumber
    FROM    dbo.PhoneNumber pn INNER JOIN dbo.Person o ON pn.PersonId = o.PersonId
    

    and

    CREATE VIEW dbo.vw_OrganisationPhoneNumber
    AS
    SELECT  pn.Id,
        pn.OrganisationId,
        pn.PhoneNumber
    FROM    dbo.PhoneNumber pn INNER JOIN dbo.Organisation o ON pn.OrganistionId = o.OrganisationId
    

    Using these views in the mapping as though they were tables enables all the normal CRUD operations while maintaing the original schema, albeit with two extra views.

    A compromise certainly but one that I think is probably justifiable when you can’t change the table structure of an existing database.

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

Sidebar

Related Questions

Ok, I think this is probably an easy question but for the life of
Ok, I think whatever I'm doing wrong, it's probably blindingly obvious, but I can't
I'd like to know how you think (or know) it is that Facebook produces
Ok, so this question will probably get closed, but hell, its 4:30 in the
as you can probably think I want to install Android ICS in VirtualBox. I
I have been having the following problem, i think it's probably due to the
This is probably going to be an easy thing but I am having a
OK, this probably is supposed to be the easiest thing in the world, but
I think the file that is produced is an .asm file, any idea how
I decided I should try to implement coroutines (I think that's how I should

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.