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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:15:44+00:00 2026-05-20T03:15:44+00:00

My simplified domain model looks something like this: public abstract class Entity<IdK> { public

  • 0

My simplified domain model looks something like this:

public abstract class Entity<IdK> 
{
    public virtual IdK Code { get; protected set; }
}

public class Contact : Entity
{
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }
}

public class Company : Entity
{
    public virtual string Name { get; set; }
}

and I’ve defined a viewmodel:

public ContactViewModel()
    {
            public Guid Code { get; set; }
            public int Version { get; set; }

            public string Name { get; set; }
            public string Company { get; set; }

            public List<SelectListItem> Companies { get; set; }
    }

to manage my contacts in a view.
Since I want the user to be able to choose from a list of companies I’ve added a list of SelectedListItem which will be rendered in my view like this:

<%=Html.ListBoxFor(m => m.Company, (List<System.Web.Mvc.SelectListItem>)Model.Companies)%>

Now, when the user submits my form I remap my viewmodel with my model before I save it.
I populate my Contact and use the id of the ContactViewModel.Company to create an object of type Company to associate with the property of the Contact class.
Since I don’t want to fetch the whole company from the database I just fill the id.
When I persist my contact, though, I get an exception: “not-null property references a null or transient Domain.Contact.Company”.
What is the best solution to manage lookups and persistence with MVC + Nhibernate?
Do you have any suggestions from your experience?

  • 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-20T03:15:44+00:00Added an answer on May 20, 2026 at 3:15 am

    Unfortunately with NHibernate and lookups you can’t just assign the ID property to a new instance of the Company object and then assign that Company object to the Contact.

    Generally what I would do is in my repository, assuming that you can’t change the Company information when saving a contact is something like this:

    public Contact Save(Contact contact)
    {
         if(contact.Company.Id > 0)
            contact.Company = Session.Load<Company>(contact.Company.Id);
    
         Session.SaveOrUpdate(contact);
    }
    

    I generally find this allows you to encapsulate the logic of loading the Company and also allows you to keep it all wrapped up nicely in a single session.

    Using Session.Load in this manner avoids hitting the database as described here

    If you don’t do this, what you’re essentially saying to NHibernate is that you have a company object which you have assigned an ID and now want to save it with all the properties set to Null or empty string values or whatever and that is not what you want.

    Alternatively you could create a Save specific Domain Object that looks like this:

    public abstract class Entity<IdK> 
    {
        public virtual IdK Code { get; protected set; }
    }
    
    public class SavableContact : Entity
    {
        public virtual string Name { get; set; }
        public virtual IdK CompanyId { get; set; }
    }
    

    Which maps directly to the Contact table in your database so that when you Save this entity you can literally just map back the CompanyId from your view model and NHibernate will only save that value back and not care at all about the company objects.

    It’s a case of working out what works best for you. I personally prefer the first option as the extra bit of logic helps simplify the domain model, however if you’re creating and exposing a public API then the second method might make more sense.

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

Sidebar

Related Questions

My simplified domain model in grails is like this: Article { Integer totalViews static
Here's a simplified version of my class: public abstract class Task { private static
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
Is there any way to get this to work? Here's a simplified/contrived illustration of
Here is a simplified version of my database model. I have two tables: Image,
I have my domain model with several NewsType's which are subclasses of NewsItem as
I have code (simplified) Configuration: _container.AddComponent<IRepository<Project>, FakeProjectRepository>(); var instance = new List<Project>(); _container.Kernel.AddComponentInstance<IList<Project>>(instance); Class:
Simplified for example, I have two tables, groups and items . items ( id,
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
My simplified and contrived example is the following:- Lets say that I want to

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.