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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:33:15+00:00 2026-06-11T19:33:15+00:00

I have a database with Car Histories. Each car history has multiple images associated

  • 0

I have a database with Car Histories. Each car history has multiple images associated with it. I am using NHibernate 2.2 to set up the relationships and the CarHistory mapping contains a bag for the images:

<bag name="Photos" table="DetailPhoto" cascade="all" lazy="true">
  <key column="CAR_DETAIL_ID"/>
  <one-to-many class="DetailPhoto"/>
</bag>

I have an iPad app which communicates with the server using JSON. I want to load all of the car history items into a list on the iPad and I don’t want to include the photos when loading the list because it slows down the data retrieval which is why I have made the Photos lazy.

When I try and use JsonConvert.SerializeObject to serialize my list of Car Histories I get a lazy initialization exception which is because I have loaded my objects and closed the session already because I don’t need the photos and the JsonSerializer is touching all of the properties on the object.

I would like to return the Json data to the client without the photos but I cannot use the ignore JsonProperty on my object because I want to load this collection in other situations.

I have tried this but it just does the same thing giving me the lazy initialization exception:
http://www.royjacobs.org/2011/07/27/using-json-net-to-serialize-proxied-nhibernate-objects/

This is my CarHistory (CarDetail) class

 public class CarDetail
{
    [JsonProperty("id")]
    public virtual int Id { get; set; }

    [JsonProperty("carId")]
    public virtual int CarId { get; set; }

    [JsonProperty("date")]
    public virtual DateTime ? Date { get; set; }

    [JsonProperty("details")]
    public virtual string Details { get; set; }

    [JsonProperty("photos")]
    public virtual IList<DetailPhoto> Photos { get; set; }
}

So my question is how can I retrieve a list of CarHistories without the associated Photos in some circumstances and not others?

  • 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-11T19:33:17+00:00Added an answer on June 11, 2026 at 7:33 pm

    For cases like this you often want to come up with a layer in-between your "connected" domain objects and the serialization format. I usually create a data transfer object, which is similar to the domain model object, but usually doesn’t have direct associations and instead just has lists of IDs. I then use Automapper to map between the data transfer object and the domain object.

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

Sidebar

Related Questions

I have a secondhand sale car database and four tables with relationships. Same column
I have a car model. (In my database, the cars table has a column
I have a database of suppliers, they rent car. Most people rent for a
I have database with multiple tables in Microsoft SQL Server with schema in tables
I have a Car class. It has three properties: id, color and model. In
I have a SQL Server 2008 database that has a Trigger. This trigger is
I have two classes: public class RichMan { public string ID {get;set;} List<Car> _cars=new
If it matters, I'm using Firebird 2.1 database. I have three tables, one with
I have two methods that are overloads of each other public class Car {
Excuse the long question! We have two database tables, e.g. Car and Wheel. They

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.