<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Discard changes in LINQ to SQL DataContext</title>
	<atom:link href="http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/feed" rel="self" type="application/rss+xml" />
	<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext</link>
	<description></description>
	<pubDate>Tue, 07 Sep 2010 20:24:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Graeme</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-701</link>
		<dc:creator>Graeme</dc:creator>
		<pubDate>Mon, 28 Jun 2010 15:24:59 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-701</guid>
		<description>The only reason I can think of that would cause that line to fail in C# is that you need to have parenthesis at the end of the GetType function.  Here's my conversion of that function.  Sorry, the formatting will be all screwy.

        public void DiscardInsertsAndDeletes(DataContext data)
        {
            var changes = data.GetChangeSet();

            foreach (var insertion in changes.Inserts)
            {
                data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);
            }

            foreach (var deletion in changes.Deletes)
            {
                data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);
            }
        }</description>
		<content:encoded><![CDATA[<p>The only reason I can think of that would cause that line to fail in C# is that you need to have parenthesis at the end of the GetType function.  Here&#8217;s my conversion of that function.  Sorry, the formatting will be all screwy.</p>
<p>        public void DiscardInsertsAndDeletes(DataContext data)<br />
        {<br />
            var changes = data.GetChangeSet();</p>
<p>            foreach (var insertion in changes.Inserts)<br />
            {<br />
                data.GetTable(insertion.GetType()).DeleteOnSubmit(insertion);<br />
            }</p>
<p>            foreach (var deletion in changes.Deletes)<br />
            {<br />
                data.GetTable(deletion.GetType()).InsertOnSubmit(deletion);<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-700</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Mon, 28 Jun 2010 06:45:49 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-700</guid>
		<description>Can anyone convert the DiscardInsertsAndDeletes() and DiscardUpdates() methods to C#?  I can't get data.GetTable(insertion.GetType).DeleteOnSubmit(insertion)
 to work properly.  Thank you.</description>
		<content:encoded><![CDATA[<p>Can anyone convert the DiscardInsertsAndDeletes() and DiscardUpdates() methods to C#?  I can&#8217;t get data.GetTable(insertion.GetType).DeleteOnSubmit(insertion)<br />
 to work properly.  Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Svercek</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-640</link>
		<dc:creator>John Svercek</dc:creator>
		<pubDate>Thu, 01 Apr 2010 03:35:27 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-640</guid>
		<description>WOW, thanks this is great. I was just getting ready to go back to ADO. Saved me a ton of work.</description>
		<content:encoded><![CDATA[<p>WOW, thanks this is great. I was just getting ready to go back to ADO. Saved me a ton of work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Donnie Wishard</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-638</link>
		<dc:creator>Donnie Wishard</dc:creator>
		<pubDate>Tue, 30 Mar 2010 20:57:52 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-638</guid>
		<description>Thanks for this, exactly what I needed.  Saved me a great deal of time!</description>
		<content:encoded><![CDATA[<p>Thanks for this, exactly what I needed.  Saved me a great deal of time!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-637</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Fri, 26 Mar 2010 15:11:52 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-637</guid>
		<description>Exactly what I was looking for... Great Job and Thank You!!!</description>
		<content:encoded><![CDATA[<p>Exactly what I was looking for&#8230; Great Job and Thank You!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derin</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-635</link>
		<dc:creator>Derin</dc:creator>
		<pubDate>Thu, 25 Feb 2010 12:39:18 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-635</guid>
		<description>Thanks Graemehill,

I searched a lot find a solution to discard all changes, now i got.

it was really helpful

thanks</description>
		<content:encoded><![CDATA[<p>Thanks Graemehill,</p>
<p>I searched a lot find a solution to discard all changes, now i got.</p>
<p>it was really helpful</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: macias</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-629</link>
		<dc:creator>macias</dc:creator>
		<pubDate>Wed, 10 Feb 2010 12:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-629</guid>
		<description>Unfortunately I found problem with discard along with transactions. Please take a look:

begin trans
insert
update (of inserted record)
*
commit trans

in case of failure, all changes should be discarded.

However since all actions are inside transaction, on failure the insert is rolled back, and there is no way to fetch the data back to record to discard update. As the result of DiscardChanges I get exception from Linq about non existing record.

I can make a workaround for this particular case, but it would be great if someone had an idea for general, smart solution.</description>
		<content:encoded><![CDATA[<p>Unfortunately I found problem with discard along with transactions. Please take a look:</p>
<p>begin trans<br />
insert<br />
update (of inserted record)<br />
*<br />
commit trans</p>
<p>in case of failure, all changes should be discarded.</p>
<p>However since all actions are inside transaction, on failure the insert is rolled back, and there is no way to fetch the data back to record to discard update. As the result of DiscardChanges I get exception from Linq about non existing record.</p>
<p>I can make a workaround for this particular case, but it would be great if someone had an idea for general, smart solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: macias</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-624</link>
		<dc:creator>macias</dc:creator>
		<pubDate>Fri, 29 Jan 2010 11:52:47 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-624</guid>
		<description>Thank you very much for explanation. I have still problems with inserts though. Normally I work like this:
record = new Record();
db.Table.InsertOnSubmit(record);

// here setting all the values of the record

// here discard may occur (+jump off the procedure)

and despite the loop iterates through this table it does not discard changes.

As a workaround I introduced new bool value which tracks if this is new record or just edit, and just before submit I add

// here discard may occur (+jump off the procedure)
if (is_insert)
  db.Table.InsertOnSubmit(record);

This works, but thanks to not using discarding inserts.</description>
		<content:encoded><![CDATA[<p>Thank you very much for explanation. I have still problems with inserts though. Normally I work like this:<br />
record = new Record();<br />
db.Table.InsertOnSubmit(record);</p>
<p>// here setting all the values of the record</p>
<p>// here discard may occur (+jump off the procedure)</p>
<p>and despite the loop iterates through this table it does not discard changes.</p>
<p>As a workaround I introduced new bool value which tracks if this is new record or just edit, and just before submit I add</p>
<p>// here discard may occur (+jump off the procedure)<br />
if (is_insert)<br />
  db.Table.InsertOnSubmit(record);</p>
<p>This works, but thanks to not using discarding inserts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Graeme</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-623</link>
		<dc:creator>Graeme</dc:creator>
		<pubDate>Tue, 26 Jan 2010 17:50:41 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-623</guid>
		<description>Refresh() just reverts the properties on an entity to their original state based on values in the database.  Basically, it is an operation on the entities that you pass in the parameter, not on the DataContext.  The result is that it will update entity properties, but not necessarily that entity's state in the DataContext.

If encapsulation is what you're worried about, just make a functions that calls DiscardInsertsAndDeletes and DiscardUpdates.</description>
		<content:encoded><![CDATA[<p>Refresh() just reverts the properties on an entity to their original state based on values in the database.  Basically, it is an operation on the entities that you pass in the parameter, not on the DataContext.  The result is that it will update entity properties, but not necessarily that entity&#8217;s state in the DataContext.</p>
<p>If encapsulation is what you&#8217;re worried about, just make a functions that calls DiscardInsertsAndDeletes and DiscardUpdates.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: macias</title>
		<link>http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/comment-page-1#comment-621</link>
		<dc:creator>macias</dc:creator>
		<pubDate>Tue, 26 Jan 2010 10:06:36 +0000</pubDate>
		<guid isPermaLink="false">http://graemehill.ca/?p=5#comment-621</guid>
		<description>Thank you for useful post -- one question though. Why the same method with refresh does not work for inserts? I.e. I would like to make the discarding procedure uniform for all three types, so I thought the best pick would be to refresh all tables -- altered by delete, insert or update.</description>
		<content:encoded><![CDATA[<p>Thank you for useful post &#8212; one question though. Why the same method with refresh does not work for inserts? I.e. I would like to make the discarding procedure uniform for all three types, so I thought the best pick would be to refresh all tables &#8212; altered by delete, insert or update.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
