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

  • Home
  • SEARCH
  • 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 6583287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:23:21+00:00 2026-05-25T16:23:21+00:00

I am using this article to make a one-to-one relationship between my two objects

  • 0

I am using this article to make a one-to-one relationship between my two objects – Site and WebOptions. Site is already present. a record in WebOptions may or may not be present. When it is, my mappings work fine. When it is not, my system blows up trying to create a new record.

Here is my site class (the important bits)

public class Site : CoreObjectBase
{
    public virtual int SiteId { get; set; }
    public virtual WebOptions WebOptions { get; set; }
}

And here is my web options class (important parts again)

public class WebOptions : CoreObjectBase
{
    public virtual int WebOptionsId { get; set; }

    private int SiteId { get; set; }
    private Site Site { get; set; }
}

And the mapping for Site is

HasOne<WebOptions>(x => x.WebOptions)
    .Cascade.All();

And the mapping for WebOptions is

Id(Reveal.Property<WebOptions>("SiteId")).GeneratedBy.Foreign("Site");
HasOne<Site>(Reveal.Member<WebOptions, Site>("Site"))
    .Constrained()
    .ForeignKey();

In the data, the table behind Site has no foreighn key field to WebOptions, but the table behind WebOptions contains the SiteId. In my code, I am already getting the site and use site.WebOptions.SomeSetting and would like to keep it that way.

My problem is this. If I deviate from this mapping at all, my model breaks and no weboptions are returned while several records are saved into the weboptions table (duplicates). But, when I try to save a new WebOptions object, I get

Batch update returned unexpected row count from update; actual row
count: 0; expected: 1

I have a repository class with 2 save methods:

public sealed class Repository<T> : IRepository<T> where T : CoreObjectBase
{
    public void SaveWithDependence<K>(T entity, K dependant) where K : CoreObjectBase
    {
        entity.Validate();
        dependant.Validate();

        using (ITransaction tx = Session.BeginTransaction())
        {
            Session.SaveOrUpdate(entity);
            Session.SaveOrUpdate(dependant);
            tx.Commit();
        }
    }

    public void Save(T entity)
    {
        entity.Validate();

        using (ITransaction tx = Session.BeginTransaction())
        {
            Session.SaveOrUpdate(entity);
            tx.Commit();
        }
    }
}

When no WebOptions is found, I am doing this when making a new one:

var options = site.WebOptions;

if (options == null)
{
    options = new WebOptions(site);
    site.WebOptions = options;
}

And the constructor looks like this to set the private variables

public WebOptions(Site site)
{
    Site = site;
    SiteId = site.SiteId;
}

And then to save, I have tried to following:

siteRepository.Save(site);

and

siteRepository.SaveWithDependence(site, options);

and

optionsRepository.Save(options);

and

optionsRepository.SaveWithDependence<Site>(options, site);

All of them return the above error. My session declaration looks like this

sessionFactory =  
            Fluently.Configure().Database(
                FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005.DefaultSchema("dbo")
                    .ConnectionString(c => c
                        .FromConnectionStringWithKey("MyDatabase"))
                        .AdoNetBatchSize(20))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionManager>())
                    .ExposeConfiguration(x => x.SetProperty("current_session_context_class", "managed_web"))
                    .BuildSessionFactory();

I really need to be able to save a new WebOptions record if one doesn’t exist, but I can’t seem to get it to work with my one-to-one relationship.

  • 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-25T16:23:21+00:00Added an answer on May 25, 2026 at 4:23 pm

    Wow, I spent all that time putting that together and then in playing around with it, I removed one line of code – just to see what would happen.

    In the constructor for WebOptions I removed this single line:

    SiteId = site.SiteId;
    

    for a constructor that looks like this:

    public WebOptions(Site site)
    {
        Site = site;
    }
    

    Then, I save only my WebOptions object like this:

    optionsRepository.Save(options);
    

    My best guess is that since I am using an ID field for the ‘SiteId’ property in fluent, fluent doesn’t allow me to manually set that value. Setting the private property Site in addition to setting the Site.WebOptions property, must set up the one-to-one relationship for fluent/nhibernate to deduce what value to place into the SiteId field.

    Further inspection of the article posted above shows that this is the way it has to be done. I just happened to miss this very important piece of information:

    The public constructor, taking a Client parameter, is the one you will use in your code whenever you want to assign a client some alimentary habits, such as: AlimentaryHabits = new AlimentaryHabits(this);. The protected constructor is used internally by NHibernate, and must be present. You can completely ignore it.

    I am going to leave this post and answer in the event someone else has this issue and I can save them a little bit of time and frustration.

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

Sidebar

Related Questions

I'm using this article to start a process on a remote machine using WMI.
I wrote my code using this article at msdn as a primary helper My
i m using this article http://blogs.msdn.com/delay/archive/2009/09/23/if-it-walks-like-a-duck-and-talks-like-a-duck-it-must-be-a-treegrid-a-simple-xaml-only-treegrid-ui-for-wpf.aspx to have hierarchical data... i am having treeview
When I saw this article about using a python-like sintax (indentation based, without curly
Based on this article , it seems like SO is using Javascript OpenID Selector
LearnWPF.com posted this article about converting bitmap images to XAML and using them in
I want to implement an apperance as this article mentioned using nested ListView control.
First let me say I have read this useful article thoroughly and am using
I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
I'm really stuck on this one... Basically, I'm trying to make 2 pages always

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.