<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Platform Specific Issues|Disconnected Objects|Merging Changes" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Wrong Approach</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../Resources/Code/prettify.js"> </script> <script src="../../Resources/Code/lang-vb.js"> </script> <script src="../../Resources/Code/jquery.min.js"> </script> <script src="../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../index_CSH.html#platform_specific_issues/disconnected_objects/wrong_approach.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../platform_specific_issues.htm">Platform Specific Issues</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../disconnected_objects.htm">Disconnected Objects</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="merging_changes.htm">Merging Changes</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Wrong Approach</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>Wrong Approach</h1> <p>The wrong approach is to try to store disconnected objects. db4o manages object by their <a href="../../basics/identity_concept.htm">object-identity</a> and doesn't recognize objects which have been serialized or loaded by another object container instance. This example shows, that instead of updating the object, db4o will store a new instance of the object. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">Pilot joe; using (IObjectContainer container = OpenDatabase()) { joe = QueryByName(container, "Joe"); } // The update on another object joe.Name = "Joe New"; using (IObjectContainer otherContainer = OpenDatabase()) { otherContainer.Store(joe); } using (IObjectContainer container = OpenDatabase()) { // instead of updating the existing pilot, // a new instance was stored. IList<Pilot> pilots = container.Query<Pilot>(); Console.WriteLine("Amount of pilots: " + pilots.Count); foreach (Pilot pilot in pilots) { Console.WriteLine(pilot); } }</pre> <div class="codesnippet-meta">ObjectIdentityExamples.cs: Update doesn't works when using the different object containers <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/objectidentity/Example-disconnectedobj-objectidentity-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim joe As Pilot Using container As IObjectContainer = OpenDatabase() joe = QueryByName(container, "Joe") End Using ' The update on another object joe.Name = "Joe New" Using otherContainer As IObjectContainer = OpenDatabase() otherContainer.Store(joe) End Using Using container As IObjectContainer = OpenDatabase() ' instead of updating the existing pilot, ' a new instance was stored. Dim pilots As IList(Of Pilot) = container.Query(Of Pilot)() Console.WriteLine("Amount of pilots: " & pilots.Count) For Each pilot As Pilot In pilots Console.WriteLine(pilot) Next End Using</pre> <div class="codesnippet-meta">ObjectIdentityExamples.vb: Update doesn't works when using the different object containers <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/objectidentity/Example-disconnectedobj-objectidentity-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>So in order to update an object, you need to load and store it in the same object-container. If you cannot do this, you need to merge to object-changes. <a href="example_merge_changes.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Example Merge Changes"</a></p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">using (IObjectContainer container = OpenDatabase()) { Pilot joe = QueryByName(container, "Joe"); joe.Name = "Joe New"; container.Store(joe); } using (IObjectContainer container = OpenDatabase()) { IList<Pilot> pilots = container.Query<Pilot>(); Console.WriteLine("Amount of pilots: " + pilots.Count); foreach (Pilot pilot in pilots) { Console.WriteLine(pilot); } }</pre> <div class="codesnippet-meta">ObjectIdentityExamples.cs: Update works when using the same object container <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/objectidentity/Example-disconnectedobj-objectidentity-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Using container As IObjectContainer = OpenDatabase() Dim joe As Pilot = QueryByName(container, "Joe") joe.Name = "Joe New" container.Store(joe) End Using Using container As IObjectContainer = OpenDatabase() Dim pilots As IList(Of Pilot) = container.Query(Of Pilot)() Console.WriteLine("Amount of pilots: " & pilots.Count) For Each pilot As Pilot In pilots Console.WriteLine(pilot) Next End Using</pre> <div class="codesnippet-meta">ObjectIdentityExamples.vb: Update works when using the same object container <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/objectidentity/Example-disconnectedobj-objectidentity-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>