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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:58:53+00:00 2026-05-18T09:58:53+00:00

I have an entity that is made up of properties from two different tables

  • 0

I have an entity that is made up of properties from two different tables as I described here and I’m running into problems when I try to insert a new item. When inserting I need to only update the fields in one of the two tables. Using the ReadOnly() method, I’ve been able to get NHibernate to ignore most of the fields from [RegistrationField] on save. However, I can’t get it to not try to save a new entry into [RegistrationField] for the foreign key, even though an entry already exists for that key.

My mapping for the class is:

public class RegistrationFieldMap : ClassMap<RegistrationField>
{
    public RegistrationFieldMap()
    {
        Table("AccountRegistrationField");

        Id(r => r.ID).Column("RegistrationID");
        Map(r => r.AccountID);
        Map(r => r.DefaultValue);
        Map(r => r.FieldID);
        Map(r => r.IsRequired);
        Map(r => r.Label);
        Map(r => r.Priority);
        Join("RegistrationField", t =>
        {
            t.Map(r => r.FieldType).ReadOnly();
            t.Map(r => r.HtmlID).ReadOnly();
        });
    }
}

When I run my test to verify the mapping, NHibernate tries to run the following two SQL statements:

INSERT INTO AccountRegistrationField (
    AccountID, 
    DefaultValue, 
    FieldID, 
    IsRequired, 
    Label, 
    Priority) 
VALUES (@p0, @p1, @p2, @p3, @p4, @p5); 
select SCOPE_IDENTITY();
@p0 = 1 [Type: Int32 (0)], 
@p1 = 'bar' [Type: String (4000)], 
@p2 = 1 [Type: Int32 (0)], 
@p3 = False [Type: Boolean (0)], 
@p4 = 'bar' [Type: String (4000)], 
@p5 = 1 [Type: Int32 (0)]

INSERT INTO RegistrationField (UserRegistrationField_id) VALUES (@p0);
@p0 = 12 [Type: Int32 (0)]

I only need it to run the first statement, since the [RegistrationField] table contains a static list of values and new items should never be added to it.

Any and all suggestions welcome.

  • 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-18T09:58:54+00:00Added an answer on May 18, 2026 at 9:58 am

    OK, after thinking about Diego’s comment and getting a good nights sleep I figured out the correct way to implement this and get the result I want. Diego is right that I should be using Reference and not Join. My original reasoning for wanting to implement this as a join was a desire to avoid adding another level to my object graph. AccountRegistrationField is already a property on the Account object and I didn’t want to have to make a call like Account.AccountRegistrationField.RegistrationField.FieldType. I instead wanted the call to be Account.AccountRegistrationField.FieldType since the object I’ll really be operating on is the AccountRegistrationField object.

    I was able to get this behavior by designing my AccountRegistrationField class a little better. I made the RegistrationField property private and added public properties on the AccountRegistrationField class to expose the fields that I needed. And then used Fluent NHibernate’s Reveal class to expose the private property to my mapping.

    My AccountRegistrationField class ended up looking like this:

    public class AccountRegistrationField
    {
        public virtual int ID { get; set; }
        public virtual int AccountID { get; set; }
        public virtual string DefaultValue { get; set; }
        public virtual int FieldID { get; set; }
        public virtual string HtmlID 
        { 
            get { return RegistrationField.HtmlID; } 
        }
        public virtual bool IsRequired { get; set; }
        public virtual string Label { get; set; }
        public virtual int Priority { get; set; }
        public virtual string FieldType 
        { 
            get { return RegistrationField.FieldType; } 
        }
    
        private RegistrationField RegistrationField { get; set; }
    }
    

    And my mapping for AccountRegistrationField looks like:

    public class AccountRegistrationFieldMap : ClassMap<AccountRegistrationField>
    {
        public AccountRegistrationFieldMap()
        {
            Table("AccountRegistrationField");
    
            Id(r => r.ID).Column("RegistrationID");
            Map(r => r.AccountID);
            Map(r => r.DefaultValue);
            Map(r => r.IsRequired);
            Map(r => r.Label);
            Map(r => r.Priority);
            References<RegistrationField>(
                     Reveal.Member<AccountRegistrationField>("RegistrationField"))
                .Column("FieldID");
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity Word, and the are two properties in it - inputSound
I have a core data model with an entity called clients that is made
I currently have an Entity Framework model that collects data from a legacy database
I have an Entity that holds the last instance of a Component: @Entity public
I have an entity that has a few relations. I need to clone this
I have an entity that has a collection in it. The collection is a
I have an entity that contains a list of other entities. The list of
Imagine you have an entity that has some relations with other entities and you
Let's say we have an entity that has attributes att1 and att2, where att1
in an app i have an entity that contains a list of other 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.