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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:47:16+00:00 2026-05-13T14:47:16+00:00

I have to 2 entities like this: class A { int id { get;

  • 0

I have to 2 entities like this:

class A
{
   int id { get; set; }
   string Name { get; set; }
}

class B
{
   int id { get; set; }
   A RefToA { get; set; }
   string Name { get; set; }
}

How can I map this 2 classes so that i would have 3 tables like this:

  • table A with 2 columns: id and name

  • table B with 2 columns: id and name

  • table AB with 2 columns: AId and BId

  • 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-13T14:47:16+00:00Added an answer on May 13, 2026 at 2:47 pm

    If I understand this correct you are creating a ref table because you want the reference to be nullable. If that is the case, you do not need a ref table. Simply set the FK in table b as nullable. Then you can map it a simple reference. Then you would have tables like this:

    • table A with 2 columns: id and name
    • table B with 2 columns: id, name and
      Aid (nullable)

    And you can map it like this:

    public class AMap : ClassMap<A>
    {
      public AMap()
      {
          Id(x => x.Id); 
          Map(x => x.Name);
      }
    }
    
    public class BMap : ClassMap<B>
    {
      public BMap()
      {
         Id(x => x.Id); 
         Map(x => x.Name);
         References(x => x.RefToA);
      }
    }
    

    Update

    There is no way of mapping this how you want in nhibernate (and no other orm for that matter). The reason for this is quite simple: it violates quite a few rules and there is never a reason to do it this way. The correct way to do this is to have a nullable fk reference in table b. That is how you represent a reference in a sql database. It is simply bad design to use many-to-many when you mean one-to-many and it will most certainly give you trouble later on.

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

Sidebar

Related Questions

I have two entities like this: public class Service { public virtual int ServiceId
I have two entities like: public class Employee { public int Id { get;
I have a collection of entities with an any-association, like this: public class CreatedLog
I have code like this: public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests) { var documentSession = _documentStore.OpenSession();
Currently I have my entities working with models that look like: public class MyModel
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have a SQL Server Stored Procedure that looks like this: CREATE PROCEDURE [dbo].[my_stored_procedure]
I have two simplest of Entities, similar to: public class User { int id;
public class Car { public string SomeProperty { get; set; } public Manufacturer Manufacturer
I have a large table that I want to map to a few entities.

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.