I’ve customized the EF T4 code generation following these steps. All good, but It seems that the EF Designer view, where you see the tables as “entities” and the mapping between the properties and the columns in the table. My code shows the changes I make in the T4 template but the designer doesn’t. Also, it doesn’t update the code if I add a new table to the designer. Is there something I’m missing?
Edit:
The customization I’ve done so far is adding a custom property to each of the generated entities. Below is the T4 code:
region.Begin(GetResourceString("Template_RegionPrimitiveProperties"));
WriteLine("");
Write("\t");
WriteLine("[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]");
Write("\t");
WriteLine("[DataMemberAttribute()]");
Write("\t");
WriteLine("public Guid OriginalId { get; set; }");
foreach (EdmProperty property in entity.Properties.Where(p => p.DeclaringType == entity && p.TypeUsage.EdmType is PrimitiveType))
All the “Write” and “WriteLine” calls are my own. I know is not the best way to do it but I’m still getting the hang of T4. If the way I’m writing this is the problem, please point me to the right direction on how I should add this custom property.
The designer displays off of the edmx XML, not the C# code it generates. So you may be modifying the generated code…but that doesn’t affect what the designer displays.
Further…if you try to rewrite the edmx XML itself with your customization so that it does display in the designer, you’ve got to make sure you map your new property correctly in the csdl, msl, and ssdl, or the edmx won’t validate.
BTW, it’s not that terrible to modify the edmx xml using your T4 template either. Here’s an example I wrote to automatically generate and update the edmx with a TPH (table per heirarchy) SSDL and MSL any time the edmx is saved (using some templates from the Database Generation Power Pack). This lives in my entity generation T4 template, which gets called whenever the edmx is saved.