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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:16:50+00:00 2026-06-10T14:16:50+00:00

I’m looking for a way of adding an association between two entities and having

  • 0

I’m looking for a way of adding an association between two entities and having a settable Id for the foreign key. I have searched through previous posts, but the closest I can find is a suggestion to .Load the association – which isn’t what I’m hoping for. I know this can be done in Entity Framework with the .HasForeignKey binding, but I can’t seem to find a way to do it in Fluent NHibernate.

Take the two example entities:

public class Ticket
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string ServiceId { get; set; }
    public virtual Service Service { get; set; }
}

public class Service
{
    public virtual string Id { get; set; }
}

I want to be able to create a new instance of Ticket and assign a Service to it using the following means (assume that the associated Service already exists in the table):

Ticket ticket = new Ticket() {
    Title = "Problem with MS Word",
    ServiceId = "Microsoft Word 2012"
};

What I don’t want to do is the following:

Ticket ticket = new Ticket() {
    Title = "Problem with MS Word",
    Service = Session.Load<Service>("Microsoft Word 2012")
};

I do have valid reasons for this, and like I’ve said this can be done in Entity Framework, but I’m really stumped as to how to achieve the same thing in Fluent NHibernate. My mappings currently look like this:

public class TicketMapping : ClassMap<Ticket>
{
    public TicketMapping()
    {
        Id(m => m.Id);
        Map(m => m.Title).Column("Title");
        Map(m => m.ServiceId).Column("ServiceId");
        HasOne(m => m.Service).ForeignKey("ServiceId");

        Schema("dbo");
        Table("Tickets");
    }
}

public class ServiceMapping : ClassMap<Service>
{
    public ServiceMapping()
    {
        Id(m => m.Id);

        Schema("dbo");
        Table("Services");
    }
}

Any help always appreciated!


Just a quick edit for Jay – the reason I don’t want to Session.Load my element is because I don’t want my presentation layer (MVC 3) knowing anything about NHibernate – therefore I’m using a repository pattern and injecting a single repository into the controller. So for example, I’ll have a TicketRepository which adheres to the following contract

public interface IRepository<T>
{
    T GetById(object id);
    void Create(T entity);
    void Update(T entity);
    void Delete(T entity);
}

I don’t want to have to inject a ServiceRepository also just to get a reference to the Service for the Ticket.

  • 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-06-10T14:16:52+00:00Added an answer on June 10, 2026 at 2:16 pm

    The way I see it you can’t really avoid using Session.Load(id) when using NHibernate. As mentioned in the comments this will NOT hit the database, just create a proxy object with the id.

    Some possible options:

    1. Inject a second generic repository (ServiceRepository) into the controller. I can’t really see an issue with that, but for some reason you want to avoid it. You could add a LoadById method to the generic interface and implement that differently in each implementation for NH and EF (or others). In EF impl that method may work just like GetById, while in NH impl it calls Session.Load
    2. Implement a non-generic repository for the AR (aggregate root) which in this case would be Ticket. This could have specific methods to Load a Service as well as Ticket.
    3. Implement another abstraction on top of the two repositories and inject that into the controller instead of the two repositories in option 1. This could f.ex be a persistance ignorant UnitOfWork as outlined here: Persistance ignorant UoW, or some kind of Application Service that orchestrates the creation of Tickets, or a TicketFactory.

    Of the 3 options, option 1 is probably the simplest, while 3 might provide better abstraction and more maintainability down the road.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.