<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx</link><description>Once again a question in the EF MSDN Forum has prompted a blog post where I can give a more complete answer.&amp;#160; If I understand things correctly, the person asking the question wanted a simple way to add business logic hooks to their entities which</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10235234</link><pubDate>Wed, 09 Nov 2011 04:20:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10235234</guid><dc:creator>Ben Cheng</dc:creator><description>&lt;p&gt;If you are using the June 2011 CTP update (I needed it to use ENUM in the entities -- how can you possibly live without ENUMS????). Anyways, this will kill your beautiful TT file. The simple fix is to open up the EntityHOoks.tt file in the project and replace the include file with this file &amp;quot;EF.Utility.CS.OOB.ttinclude&amp;quot;. Then it works again beautifully.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10235234" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10152153</link><pubDate>Mon, 11 Apr 2011 17:15:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10152153</guid><dc:creator>Daniel Simmons - MSFT</dc:creator><description>&lt;p&gt;@Srinivas,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure what&amp;#39;s causing this difficulty. &amp;nbsp;Are you running VS2010? &amp;nbsp;Have you restarted visual studio? &amp;nbsp;Do you see other code gen items? &amp;nbsp;If so, which?&lt;/p&gt;
&lt;p&gt;- Danny&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10152153" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10152107</link><pubDate>Mon, 11 Apr 2011 15:46:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10152107</guid><dc:creator>srinivas</dc:creator><description>&lt;p&gt;Hi Danny,&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve installed the VSIX for EntitiyHooks but it doesn&amp;#39;t show up in the list when try to code gen the model. Help!&lt;/p&gt;
&lt;p&gt;Thanks in advance.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10152107" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10090430</link><pubDate>Sat, 13 Nov 2010 00:00:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10090430</guid><dc:creator>Aaron</dc:creator><description>&lt;p&gt;Nevermind on that last post, I just figured it out. &lt;/p&gt;
&lt;p&gt;File: Hooks.tt&lt;/p&gt;
&lt;p&gt;&amp;lt;code&amp;gt;&lt;/p&gt;
&lt;p&gt;// Emit Entity Types&lt;/p&gt;
&lt;p&gt;foreach (EntityType entity in ItemCollection.GetItems&amp;lt;EntityType&amp;gt;().Where(e =&amp;gt; e.BaseType == null).OrderBy(e =&amp;gt; e.Name)) { ... }&lt;/p&gt;
&lt;p&gt;&amp;lt;/code&amp;gt;&lt;/p&gt;
&lt;p&gt;I added the &amp;nbsp;.Where(e =&amp;gt; e.BaseType == null) to the emitter.&lt;/p&gt;
&lt;p&gt;Hope this can help someone else as well!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10090430" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10073711</link><pubDate>Sat, 09 Oct 2010 22:52:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10073711</guid><dc:creator>Daniel Simmons - MSFT</dc:creator><description>&lt;p&gt;It really should work the same with self tracking entities as long as you reattach the entities / apply changes before you call SaveChanges. &amp;nbsp;The EF always uses the object state manager to figure out what changes it needs to send to the database, and these hooks use that same mechanism. &amp;nbsp;So if GetObjectStateEntries isn&amp;#39;t returning any items, then saving won&amp;#39;t have anything to work with either. &amp;nbsp;Hmmm... &amp;nbsp;I suppose the other possibility is that you might need to call DetectChanges first if you are using POCOs and the changes have been made while the entities were attached (not in self-tracking mode).&lt;/p&gt;
&lt;p&gt;- Danny&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10073711" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10073652</link><pubDate>Sat, 09 Oct 2010 17:17:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10073652</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;works great with the default entityobject generator. &amp;nbsp;However, &amp;nbsp;when i swtich to self tracking entities, it doesn&amp;#39;t seem to work. &amp;nbsp; ObjectStateManager.GetObjectStateEntries does not return any items. &amp;nbsp;Is there a differenty approach that could be used with self tracking entities?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10073652" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10010565</link><pubDate>Mon, 10 May 2010 22:00:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10010565</guid><dc:creator>Daniel Simmons - MSFT</dc:creator><description>&lt;p&gt;This all depends on how you have authentication set up, and it's really not related to the EF or to the question of how your projects are structured. &amp;nbsp;For many auth schemes, you can use System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() to get the current identity for asp.net.&lt;/p&gt;
&lt;p&gt;You might take a look at a post like this: &lt;a rel="nofollow" target="_new" href="http://blogs.iis.net/sakyad/archive/2008/11/19/process-and-thread-identity-in-asp-net-a-practical-approach.aspx"&gt;http://blogs.iis.net/sakyad/archive/2008/11/19/process-and-thread-identity-in-asp-net-a-practical-approach.aspx&lt;/a&gt; or something on MSDN for more info.&lt;/p&gt;
&lt;p&gt;- Danny&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10010565" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10010150</link><pubDate>Mon, 10 May 2010 11:35:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10010150</guid><dc:creator>MrDaveM</dc:creator><description>&lt;p&gt;With regards to adding ModifiedBy, CreatedBy etc, whats the recommended way to get the currently logged in web user when the EF Model is in a separate project?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10010150" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10005246</link><pubDate>Fri, 30 Apr 2010 14:25:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10005246</guid><dc:creator>Daniel Simmons - MSFT</dc:creator><description>&lt;p&gt;You shouldn't need to modify the generated code at all to add your logic. &amp;nbsp;First off, keep in mind that these are partial classes so you can put your own code in a different file and have it be part of the same class. &amp;nbsp;Secondly, the generated code declares partial methods but doesn't implement them. &amp;nbsp;So you just need to implement the partial methods in your own file, and the generated code will call it. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example I mention above with Customer.OnDeleted, in that project I actually had three separate files which all contribute to the Customer class. &amp;nbsp;One file was generated as part of the default codegen and had the main code for the customer object including its properties, etc. &amp;nbsp;Another file was generated using the EntityHooks code generator from this post, and it declares the partial methods and calls them, and the third file I wrote by hand to contain my business logic. &amp;nbsp;If I change the model, then the first two files will be regenerated, but my business logic won't be touched at all.&lt;/p&gt;
&lt;p&gt;- Danny&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10005246" width="1" height="1"&gt;</description></item><item><title>re: Generating Business Logic “Hooks” for EF4 Entities</title><link>http://blogs.msdn.com/b/dsimmons/archive/2010/04/22/generating-business-logic-hooks-for-ef4-entities.aspx#10005156</link><pubDate>Fri, 30 Apr 2010 12:27:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10005156</guid><dc:creator>suntereo</dc:creator><description>&lt;p&gt;Wow! This is great!! I'm wondering if there's a way to modify the .tt file so that it only creates the generated classes if it does not yet already exist. &amp;nbsp;The problem I see is that if I add code to the generated files to handle my logic, and then add some additional tables, I need to re-run the tool. &amp;nbsp;But when I re-run the tool, it generates all of the files again, wiping out any changes. &amp;nbsp;Is this easy to do?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10005156" width="1" height="1"&gt;</description></item></channel></rss>