Using ObjectContext / DbContext inside T4 Text Template - visual-studio-2010

I've been searching for an example on how to use Entity Framework (ObjectContext/DbContext) inside of T4 template. How can I achieve this? Currently in my T4 I need to get values from database tables to generate files.

T4 template is just C# or VB.NET code inside some markup so you will use it in the exactly same way as you do that in any other code file.

Related

Can we use tt templates instead of T4 templates in Custom Scaffolding

Can we use tt templates instead of T4 templates in Custom Scaffolding for generating Controllers & views.
Or do we have a tool for converting tt templates to t4 templates ?
Please suggest.
Thanks & Regards
Christena
The tt templates are t4 templates, just the file extension is tt.
See the location of the files:
How do I create my own Scaffold Template in ASP.NET MVC 3?

.NET Entity Framework 4.0 - prevent overwrite

I have a ASP.NET 4.0 Web Application Project, it work with edmx model file generate by MSSQL 2008 and named StaffModel.edmx
I have my custom logic and apply to StaffModel.Designer.cs ,today i open the edmx ,but not re-generate form database, just have a look, but VS2010 overwrite my *.Designer.cs in accident!
No message or confirm to ask me overwrite or not, so how can i disable that in VS2010?
Generally you cannot. .Designer.cs files are autogenerated and should never be modified because every save to EDMX (even after moving some entity in diagram) or T4 template (if you use it) will trigger recreation.
Either move your changes to your own partial classes for your entities or add them directly to code generator - T4 template.

How do I disconnect code generated by a T4 template from that template?

When I 'run' a T4 template, the class files it generates for me all appear under the template. I cannot remove the template from the project without the files going away too. I also can't copy the files out from 'under' the template into the project root because the are already in the project root. How can I emancipate these files from the template that created them?
Get creative:
Right click and open containing folder.
Drag-and-drop the generated file to another location in the solution.
Delete the original template and file from Visual Studio.
Presto.
If you want to do one-time code-generation with T4 then I would suggest using T4 scaffolding. This tool was originally created to support the generation of views, controllers etc. in MVC3 but you can use it on pretty much anything.

How to automatically run custom tool for T4 file when another file is saved

Is there a way to automatically run the T4 transform when another file is saved?
The AutoRunCustomTool Visual Studio extension was created to address this exact scenario.
It seems like T4 Toolbox is not available for VS2012. However there is now a feature in tangible T4 Editor that allows you to simply add a auto-transformed template to any exisitng file in your solution so that when the parent file is saved the dependend t4 file is transformed. See here:
http://t4-editor.tangible-engineering.com/blog/dependent-t4-templates.html
You can do that with T4 Toolbox. Select the file in Solution Explorer and set the Custom Tool Template in the Properties window.
I get this answer form How can set the custom tool property of an xml file to a T4 file?

Is it possible to generate XML from a EDMX model?

I'm generating my POCO's from edmx model, and would like to know if its possible to make use of the neat codegen features of Entity Framework to create my an xml document along with the POCO (which is needed for my project).
You can generate an XML document in Visual Studio using T4 templates.
In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language.
But you should note that the EDMX file itself is an XML document.
Right click on your Model.edmx file
'Open With...'
Choose 'XML (Text) Editor'
There are three main sections to the file
StorageModels - describes that database
ConceptualModels - describes your code and objects
Mappings - describes how the two models relate
Look at whether the standard EDMX file contains the type of information you want. If not, parse and shred it using T4 to extract the information you're looking for.
No need to parse the EDMX file, use TiraggoEdmx, you can install it via NuGet. See http://brewdawg.github.io/Tiraggo.Edmx/ it serves up all of the metadata from your EDMX files that Microsoft hides from you, very simple, works great. I wrote it but it's 100% free, even comes with a sample POCO template.

Resources