Project Description
In short, this project provides a LINQ style data access solution for Microsoft CRM 4.0.

It provides a T4 Template that generates code leveraging LINQ to SQL and the MS-CRM 4.0 API to give a MS-CRM CRUD data access solution. Special Support for WCF RIA Services is also provided

Requirements
  1. Visual Studio 2008/2010
  2. Microsoft Dymanics CRM 4.0

Features
  1. Uses LINQ to SQL for Querying the CRM database, but uses the CRM 4.0 API for Create, Update, Delete operations - so data integrity is maintained.
  2. Refreshes the Data for each entity after a Create or Update so that values changed in plugins and any default values are set to the latest value.
  3. Can generate only selected entities, rather than the entire CRM Entity list.
  4. Support for WCF RIA services and any other technology that takes advantage of LINQ to SQL
  5. The T4 Templates are customizable, so if you don't like the way somethings done or you want to extend the functionality, it's completely transparent and you can just change it.
  6. Generation of code is done within the Visual Studio IDE, no need to call any external tools to re-generate the code when CRM Schema changes.
  7. The User Roles / Business Units security are applied to filter the data when using integrated security on your connection string.
  8. Generates all the 1:N, N:1 and N:N relationships between CRM entities.
  9. Generates all the Picklist values as statically typed variables.
  10. Can be used from an external application, a plugin or a workflow activity.

How can I try it out?
  1. Download the Source Code
  2. Edit the Template\Include\Settings.ttinclude file to point toward your MS CRM Organization and Database
  3. Click the "Transform All Templates" button (Found in the Solution Explorer Window)
  4. Edit the app.Config file in the Unit Test Project to point toward your MS CRM Organization and Database
  5. Run the unit test project
  6. ** NOTE: If you use Windows Explorer to unzip, you may need to "Unblock" all the *.dll, *.tt and *.ttinclude files through windows explorer (e.g. file properties, "Unblock" button), otherwise it will not let you generate the templates

How does it work?
  1. LINQ to SQL's SQLMetal.exe tool is invoked and all the FilteredViews are extracted from the database into DBML
  2. The DBML is manipulated, changing entity and attribute names and adding 1:N and M:M relationships between entities.
  3. The DBML is then used to Generate Standard LINQ to SQL Code in C#
  4. The LINQ to SQL Classes that are generated are extended via partial classes to offer alternative Insert, Update and Delete operations that go throught the CRM API.
  5. Check the Unit Tests to find examples of how to do some common operations.

Troubleshooting
  1. Make sure that you have System Administrator Rights in MS CRM and that you have sufficient rights to extract the database schema information in SQL Server when you transform the template.
  2. Check the SQLMetalPath in the settings.ttinclude file and make sure you have the correct path to the SQLMetal.exe tool.
  3. Check the Template\CrmEntities.SQLMetalOutput.txt file to make sure that SQLMetal Ran Correctly.
  4. If you have issues Updating or Creating, check to see what you would normally do in CRM - make sure that you are only updating those fields that you would normally do (e.g. updating status attribute directly in CRM is not allowed by CRM and hence is not allowed by this tool).
  5. If you want to debug the T4 template, please do! In order to do a debug of the template:
    1. Simply open another instance of Visual Studio
    2. Use Tools --> Attach to Process and Choose the Visual Studio instance which contains the T4 Template
    3. You cannot use standard break points, so in the T4 Template code put in the following line where you want a break point:
      1. if(System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    4. In Solution Explorer, use the "Transform All Templates" button to force execution of the template.
    5. If you get permissions errors, you may need to "Unblock" all the *.dll, *.tt and *.ttinclude files through windows explorer (e.g. file properties, "Unblock" button), otherwise it will not let you generate the templates.

Known Limitations
  1. Self referencing Many to Many Relationships are ignored
    1. If a Many to Many relationships is referencing the same entity on both ends, this relationship will not be generated. Hopefully, this is very rare - it is possible to support and I will get around to fixing it at some stage.
  2. Multi-Targeted Relationships not fully implemented
    1. When a Lookup Targets multiple entities, those targetted entities will not have a child to parent relationship, however the parent to child relationship exists.
  3. Cascading Deletes for relationships not implemented yet - you will need to manually do this for the time being.
  4. Only Generates C# Classes at this stage.

Last edited Jan 23, 2011 at 7:58 PM by mhunter, version 19