02 December 2010

Creating or modifying Entity Framework EDMX files from code: an introduction to HuagatiEDMXTools.dll

Microsoft’s Entity Framework stores models as xml-based EDMX files. Within the EDMX file, the model is divided up into the conceptual layer (CSDL) which describes the C# or VB entity classes and methods, the storage layer (SSDL) which describes the database tables/keys/views/procedures etc, the mapping layer (MSL) which describes how everything in the conceptual model maps to the storage model, and finally one portion describing the designer diagram layout.

The EDMX files are typically generated and modified by the designer in Visual Studio, by edmgen.exe/edmgen2.exe, or by third party tools such as the model comparer in Huagati DBML/EDMX Tools.

Reading/writing EDMX files

Sometimes people look for ways to generate, modify, or parse EDMX files from code. The straightforward answer to that is usually to use an XML parser; XmlDocument, XmlReader/XmlWriter, Linq-to-XML etc. Using XML parsers to read and write EDMX files works fine, but it also requires a fair bit of time spent on reading the CSDL, SSDL, and MSL specifications as well as some manual work figuring out some of the things not clearly spelt out in the specification such as how different common or uncommon mapping scenarios are best described in the MSL.

The HuagatiEDMXTools.dll library

Before developing the new Entity Framework 4 features in Huagati DBML/EDMX Tools, I decided to step away from directly using an XML parser and instead add an intermediate abstraction layer. That makes it a lot easier to work with the models and separates out most of the quirky mapping logic. The result was a separate wrapper library called HuagatiEDMXTools.dll . Underneath it still uses a XML parser, but all model artifacts are exposed as classes, properties, IEnumerables, IQueryables etc. Some related object types such as associationsets and associations are merged together, related objects available as properties, and useful queries and lookups are exposed as simple lookup properties and IQueryables/functions.

Although HuagatiEDMXTools was written to support the EFv4-related functionality in Huagati DBML/EDMX Tools, I decided to document it to allow add-in users to write their own code to create/modify/read EDMX models for scenarios that are not covered by the add-in or other tools. Although the library has been around and exposed since version 1.90 and for the most part fairly self-explanatory, the documentation for it wasn’t released until version 2.11. The documentation is in MSDN format (thanks, Sandcastle and SHFB), and is available online at http://huagati.com/edmxtools/help/ as well as in the form of a HTML help file that is installed together with the Huagati DBML/EDMX Tools add-in.

Online help screenshot

To use the HuagatiEDMXTools library from a VS project, simply add a reference to the file HuagatiEDMXTools.dll from the project references:

Adding a reference to HuagatiEDMXTools.dll

When using the library, start by initializing the licensing module by passing the license data from the add-in’s license file ( \users\[username]\Documents\Visual Studio 2010\Addins\HuagatiDBMLTools2010.dll.lic ) to the license class:

Setting license data in Huagati EDMX Tools' license class

Samples

Next, you can use the library to create, modify, read EDMX files, or simply to query for various model metrics using some of the built-in queries, or by writing new ones against the many IEnumerables and IQueryables exposed by the library.

Sample showing how to read and query metrics from an existing EDMX file

Sample 1/2 showing how to create a new EDMX file from scratch

Sample 2/2 showing how to create a new EDMX file from scratch

Download

As I mentioned above, the HuagatiEDMXTools library ships with Huagati DBML/EDMX Tools. You can download the latest version of Huagati DBML/EDMX Tools from http://huagati.com/dbmltools/ and get a license (free trial, or a full paid license) from the same site.

The online help for the library is available at http://huagati.com/edmxtools/help/ as well as in a .chm file that is installed together with Huagati DBML/EDMX Tools.

5 comments:

  1. How do I get the free trial???? Cant figure it out:(

    ReplyDelete
  2. Sorry, this product has been discontinued. As a result, trial licenses are no longer available.

    ReplyDelete
  3. In order to buy it, I need to test it somehow, right? There must be a way!

    ReplyDelete
  4. Was, this product, bought by somebody and that's the reason of discontinuation? If so, who is "Somebody"?

    ReplyDelete
  5. Nope, it wasn't bought. No conspiracy involved... :)

    ReplyDelete