- Complex Types in the EDM Designer in EF4 and a look at updating complex types in code May 21
- The much improved EDM Wizard Pluralization in VS2010 May 21
- Checking for EF to TSQL Query Compilation Changes in VS2010 Beta1 May 19
- EF4- What is and is not supported May 13
In the current version of Entity Framework, classes within the System.Data.EntityDesign namespace are responsible for the default code generation which creates the ObjectContext and various EntityObject classes from your model. You can use the same classes in this namespace to write your own code generator.
In VS2010, this changes dramatically. T4 templates, (T4=Text Template Transformation Toolkit) which are still a little known Visual Studio feature introduced into VS2008, are being used for the entity class code generation.
If you want to customize how the entity classes and objectContext are created, you can start with the default T4 Template and then modify it.
T4 syntax looks a bit like something between .NET code and HTML. There is not a lot of support built into VS2008 for it, certainly no editor. But there are editors such as VisualT4 from Clarius to help you with your T4 editing.
I have never used T4 before so I will use this blog post to be a public guinea pig for customizing entity class generation the new way.
I'll start by creating a VERY simple model using the EDM Wizard.
By default, a class file is generated using the Custom Tool defined in the model's Properties window, "EntityModelCodeGenerator".
It took me a few minutes to figure out how to get started. My instinct was to look in the Solution Explorer. But the starting point is in the designer's context menu: "Add New Artifact Generation Item..." Not very obvious but that's the one.
When you choose this, you will then get an Add New Item dialog filtered on the ADO.NET EntityObject Generator item and a default name, in my case Model1.tt which I will leave.
Next comes a scary warning. Just click OK and feel free to check "do not show this message again."
Notice that after creating this item, there are a few changes in Solution Explorer and properties.
The model class is now attached to the new Model1.tt file, not to the Model1.edmx file and the Custom Tool for the edmx file is empty (previously that was EntityModelCodeGenerator).
Now your ready to start customizing.
My goal will be a simple one. I will remove the DataContractAttribute from the EntityObject classes since I have no plans on using my model in a WCF service.
Here is the signature for my address entity created by the default generator. Note that my project is C# so the C# version of the template was added. THere is also a VB template! (Thanks guys!)
[EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="address")] [Serializable()][DataContractAttribute(IsReference=true)] public partial class Address : EntityObject{}
Open up the Model1.tt file.
Note that text which is inside <# #> is code to be executed. Text which is not contained inside the angle brackets will simply be written out to the file.
Search for DataContract which you will find near the top. DataContract is part of a set of text that just gets written to the file. In this screenshot, the text highlighted in Pepto Bismol pink is code that will be excuted to generate the entity code, whereas the text that is not highlighted is text which is written to the generated code.
Delete the line with DataContract.
Save the template. The code generation will happen automatically. You do not need to force it to happen.
Now when I open up the class file I can see that the signature of both of the entity classes from my model have changed.
[EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="address")] [Serializable()] public partial class Address : EntityObject{}
[EdmEntityTypeAttribute(NamespaceName="EF4TestDBModel", Name="Person")] [Serializable()] public partial class Person : EntityObject{}
That should get you started so that you can take control of how your model is transformed into classes to meet your domain needs.





