Reputation: 2115
Does anybody have samples for referencing a Code First DbContext from a T4 template?
I have found some T4 examples that use a .dbml as source and also ones that reference a database. I would like to loop through and build javascript files for all the classes in the context. I am having a hard time figuring out how to reference the EnvDTE variable to get the DbContext. From there I will convert to an ObjectContext and loop through the classes to generate the code.
Any ideas or examples?
Upvotes: 1
Views: 481
Reputation: 528
Information wise you could compile the Code First containing assembly down to .dll and then in T4 process load the DLL and read the data from it through reflection.
We did this kind of approach in a process where we had means to get database => serialization classes, but could not interfere that phase with T4; only after the code generation was done to serialization classes. And in that case it was easier to compile it and then read it through reflection.
If you go with this approach you have to deal with the fact that you need to build part of the application first, then use T4 generation against that to get the remaining done. In case you're creating Javascript files, it might make it easier.
You can make your reflection based T4 as "preprocessed", so that you can run it in post-build script.
EDIT: Added seminar presented case demonstration.
http://abstractiondev.wordpress.com/2012/03/09/microsoft-techdays-2012-finland-adm-materials/
Download the demonstration from Github, and look at the "T4 Demos.sln" - solution, Advanced7.tt demonstration. It parses types and properties from the given assembly name.
Upvotes: 1