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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:52:32+00:00 2026-05-19T21:52:32+00:00

Explanation: Let’s say I have an object graph that’s nested several levels deep and

  • 0

Explanation:

Let’s say I have an object graph that’s nested several levels deep and each entity has a bi-directional relationship with each other.

A -> B -> C -> D -> E

Or in other words, A has a collection of B and B has a reference back to A, and B has a collection of C and C has a reference back to B, etc…

Now let’s say I want to edit some data for an instance ofC. In Winforms, I would use something like this:

var instanceOfC;

using (var session = SessionFactory.OpenSession())
{
    // get the instance of C with Id = 3
    instanceOfC = session.Linq<C>().Where(x => x.Id == 3);
}

SendToUIAndLetUserUpdateData(instanceOfC);

using (var session = SessionFactory.OpenSession())
{
    // re-attach the detached entity and update it
    session.Update(instanceOfC);
}

In plain English, we grab a persistent instance out of the database, detach it, give it to the UI layer for editing, then re-attach it and save it back to the database.

Problem:

This works fine for Winform applications because we’re using the same entity all throughout, the only difference being that it goes from persistent to detached to persistent again.

The problem is that now I’m using a web service and a browser, sending over JSON data. The entity gets serialized into a string, and de-serialized into a new entity. It’s no longer a detached entity, but rather a transient one that just happens to have the same ID as the persistent one (and updated fields). If I use this entity to update, it will wipe out the relationship to B and D because they don’t exist in this new transient entity.

Question:

My question is, how do I serialize detached entities over the web to a client, receive them back, and save them, while preserving any relationships that I didn’t explicitly change? I know about ISession.SaveOrUpdateCopy and ISession.Merge() (they seem to do the same thing?), but this will still wipe out the relationships if I don’t explicitly set them. I could copy the fields from the transient entity to the persistent entity one by one, but this doesn’t work too well when it comes to relationships and I’d have to handle version comparisons manually.

  • 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-19T21:52:33+00:00Added an answer on May 19, 2026 at 9:52 pm

    I solved this problem by using an intermediate class to hold data coming in from the web service, then copying its properties to the database entity. For example, let’s say I have two entities like so:

    Entity Classes

    public class Album
    {
        public virtual int Id { get; set; }
        public virtual ICollection Photos { get; set; }
    }
    
    public class Photo
    {
        public virtual int Id { get; set; }
        public virtual Album Album { get; set; }
        public virtual string Name { get; set; }
        public virtual string PathToFile { get; set; }
    }
    

    Album contains a collection of Photo objects, and Photo has a reference back to the Album it’s in, so it’s a bidirectional relationship. I then create a PhotoDTO class:

    DTO Class

    public class PhotoDTO
    {
        public virtual int Id { get; set; }
        public virtual int AlbumId { get; set; }
        public virtual string Name { get; set; }
        // note that the DTO does not have a PathToFile property
    }
    

    Now let’s say I have the following Photo stored in the database:

    Server Data

    new Photo
    {
        Id = 15,
        Name = "Fluffy Kittens",
        Album = Session.Load<Album>(3)
    };
    

    The client now wants to update the photo’s name. They send over the following JSON to the server:

    Client Data

    PUT http://server/photos/15

    {
        "id": 15,
        "albumid": 3,
        "name": "Angry Kittens"
    }
    

    The server then deserializes the JSON into a PhotoDTO object. On the server side, we update the Photo like this:

    Server Code

    var photoDTO = DeserializeJson();
    var photoDB = Session.Load(photoDTO.Id); // or use the ID in the URL
    
    // copy the properties from photoDTO to photoDB
    photoDB.Name = photoDTO.Name;
    photoDB.Album = Session.Load<Album>(photoDTO.AlbumId);
    
    Session.Flush(); // save the changes to the DB
    

    Explanation

    This was the best solution I’ve found because:

    1. You can choose which properties the client is allowed to modify. For example, PhotoDTO doesn’t have a PathToFile property, so the client can never modify it.

    2. You can also choose whether to update a property or not. For example, if the client didn’t send over an AlbumId, it will be 0. You can check for that and not change the Album if the ID is 0. Likewise, if the user doesn’t send over a Name, you can choose not to update that property.

    3. You don’t have to worry about the lifecycle of an entity because it will always be retrieved and updated within the scope of a single session.

    AutoMapper

    I recommend using AutoMapper to automatically copy the properties from the DTO to the entity, especially if your entites have a lot of properties. It saves you the trouble of having to write every property by hand, and has a lot of configurability.

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

Sidebar

Related Questions

Let's say i have 2 files located in the same folder. /Test/View.cshtml <h1>File that
Let's say that you have a resource that is created and displayed entirely within
Let's say you have a type T and subtypes TSub1 , TSub2 etc. Several
Let's say you have a .NET system that needs to send out email notifications
Explanation I have a multidimensional array that is iterated over to created a categorized
The situation: Let's say I have an image A, say, 512x512 pixels, and image
First let me explain you what I mean by a nested transaction. Example: say
Let's say I have the following list of items: List<Item> items = Item.GetSomeItems(); And
Let's say we have a Lucene index having few documents indexed using StopAnalyzer.ENGLISH_STOP_WORDS_SET .
I have two websites, let's say they're example.com and anotherexample.net . On anotherexample.net/page.html ,

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.