Custom EFDAL generator for nHydrate

nHydrateis a model first ORM code generator on codeplex, and can be used to generate Entity Framework DAL layer. But unlike the designer in Visual Studio it is text based, but this is not a disadvantage as nHydrate offers greater features such as built in row auditing and incremental database management. As any development team knows version 1 of your application is easy, but version 2 is likely to have database schema changes. The incremental database management keeps a track of all SQL used to modify the database so you can migrate the schema changes to your production database with ease.

In the application I’m building the database connection string is only known at runtime, and in trying nHydrate I found the generated code did not support this for the audit system. I needed to make a change to the code generation to allow me to add my runtime connection string.

Also, in this application I want to use CSLA.net for the business layer, and want to use the CSLA code T4csla generatoron codeplex which uses T4 generation from the Entity Framework designer edmx file.  Now nHydrate does not have a edmx file because it does not need one. The EFDAL generator does generate the three csdl, ssdl and msl files, which Entity Framework generates from the edmx file.

So I have added a EFEDMX Generator Item to create this file using the same generators that produce the three files. Once you have edmx file you can right-click on the design surface when the file is open in Visual Studio and using the Add Generation Item to add the CSLA template. At present the cslaextension.tt and the resulting csla class files will be in the wrong class library – they’ll be sharing the same as the EFDAL code. I hope to resolve this later.

The attached Zip is self contained class libraries from 56617 on codeplex, the only changes I’ve made is to the links for the AssembyCommon.cs and the SNK files. Once built copy the EFDAL dll to your normal nHydrate install.

Edit: I didn’t anticipate what would happen to T4Csla’s properties when nHydrate was regenerated. The VSIX from T4Csla adds new properties to the graphic EF designer which are used to for the Csla class generation. Of course, when the edmx file is regenerated the values of those properties is lost. The obvious place for the Csla properties is within the nHydrate model, but that’s a lot of work and ongoing maintenance. I need to think of a better solution!

have fun!

nHydrate-EFGenerator.zip (1.48 mb)

Entity Framework Starter Kit alternative

 

The Entity Framework 4 has superb  features for custom code generation and there are now quite a few different T4 code generators being released. You can see them easily in VS2010 Extension Manager, and include the ADO.NET C# POCO and C# Web Site POCO Entity Generators and the one I particularly like, Rob Hallett’s method of Unit testing Entity Framework classes, see  http://blogofrab.blogspot.com/2010/08/unit-testing-entity-framework-40.html

To start writing your own generator, a Entity Framework Starter Kit is available, as documented here http://code.msdn.microsoft.com/DesignerExtStartKit  and here http://msdn.microsoft.com/en-us/library/ee373851.aspx.  However  I could not get the starter kit to show and assume it’s due to it being written for VS2010 RC and the VSIX is set up for a version of VS2010 I don’t have.

I decided therefore to recreate the starter kit and to enhance it to include a do nothing T4 template.
I’ve based the mode of operation from the VSIX available at http://t4csla.codeplex.com/.  and so thanks to that team as it proved very helpful.

The Zip is the source of the project and is not a project template, so you’ll need to download and compile it. It’s set up to debug in the experimental instance, so you’ll need the VS2010 SDK installed. Once you have VS2010 running add a Class Library project and a Entity Framework blank model.  Add an Entity from the toolbox, click on the design surface and notice the additional property called, “StarterKit design surface property”. Click on the Entity and notice the two Example properties.

Now right click on the design surface and select Add Code Generation Item….  In the Add Item dialogue that opens the StarterKit item template should now show. It will only show on the top Visual C# menu. Select it and it will add StartupExtention.tt to the project and when the model is save the t4 code will run to create a related cs file.

In developing this starter kit, the thing that caught me out was the item template’s VsTemplate must have a filename starting with, ADONETArtifactGenerator.

Hope you find this useful as a basis for your own work.
John

Update 7th May 2013
I have upgraded the VS2010 VSIX to VS2012 and it worked without issue.
Below are the Starter Kits for VS2010 and VS2012 (they will only install on their respective versions)
The VS2012 is already built and you can find the VSIX in the bin/Debug directory.
The third zip is a test program for you to see the added Properties in the Entity Designer.
Have fun!

StarterKitExtensionVS2010.zip (67.90 kb)

StarterKitExtensionVS2012.zip (300.68 kb)

StarterKitTest2ForAfterYouHaveInstalledTheVSIX.zip (2.79 mb)

At the moment the attachment links are broken.
Try getting files from my GitHub repository, https://github.com/jradxl?tab=repositories

 

Customising csproj build scripts

Here’s an item I need to blog about because in a few months I will have forgotten the details.

First, the verbosity of Visual Studio MSBuild messages. Go to Tools > Options > Project and Solutions > Build and Run. Set the build output verbosity to Normal. Would appear that the VS install default is Minimal.

Now you’ve done that, you can edit your csproj file to add some custom targets where you’ll actually see any messages in the output window, for example:-

<Target Name="BeforeBuild">
    <Message Text="My Before Build message..." />
</Target>

Finally, I found this post enhancing the AfterClean target which I thought was most helpful.
http://www.ademiller.com/blogs/tech/2007/12/deep-clean-your-build/

  <Target Name="AfterClean">
    <Message Text="Deep cleaning..." />
    <CreateItem Include="$(OutDir)*.*;$(IntermediateOutputPath)**\*.*">
        <Output TaskParameter="Include" ItemName="Clean_FilesToDelete"/>
    </CreateItem>
    <Delete Files="@(Clean_FilesToDelete)" />
    <RemoveDir Directories="$(OutDir);$(OutDir)..\;$(IntermediateOutputPath);$(IntermediateOutputPath)..\" />
  </Target>

have fun!
John