Changing default T4 template for new EDMX files - visual-studio

How do I force VS to choose a custom template when creating EF models?
Details:
I built a custom template for generating EF entity classes from models. The solution has many projects with their own models and it is likely more will be added over time.
In order to prevent bugs in the future, I'd like to force VS to choose my template by default when creating EF models in the future. I'd prefer this to be at the Solution level, so that the change is part of the repository.
I have the template exported as a VS Item Template that is basically a clone of the default EF 6.x DbContextGenerator with the appropriate values replaced. I'm able to run it manually by using the "Add Code Generation Item..." option on the model designer view.

Related

How can we get the T4 template to generate code based on a .cs file that the user is editing?

I'm trying to create a T4 template that will save our developers from creating a lot of boilerplate code that's necessary in our framework. Let's say the developer creates an interface and marks it with our custom attribute. I would like it so that any interface marked with that custom attribute is enhanced by additional methods, which means my T4 template would have to generate partial classes on the fly. However, I would like it so that this automatic generation happens on the fly and seamlessly, preferably when the internal automatic compilation that's used for intellisense happens. You know how when you create a new class in Visual Studio and you switch to another source file and start using that class you didn't have to save or compile it, Intellisense was able to see the new class you created right away? I'd like the same automatic behavior with the code generated from my T4 template. Any thoughts?
You cannot do what you want to do easily, but here are some options ordered from easiest to most likely what you want (hardest).
Create code
snippets
Create a Visual Studio Item Template
Use Castle DynamicProxy to create the extra bits at run time.
Create a separate project to hold the T4 generated classes as described in my answer here
As a pre-step to your project build (modify .csproj file to do this), you can compile the source code from which you want to generated code and then reflect on that, generate the code and then add it to the project before the real compile step. This is what the MSR Orleans project does. You can read their source code here. This is really cool! :-)

Add Helper Classes to Project By Default

Is there a way to have visual studio add my helper classes (preferably with the correct namespace) anytime I create a new project. For example all of the extension methods, conversions etc. Its a pain to add them every time I start a new project.
Thanks
I'm in VS2012
There are 2 ways:
create a project template
create a set of library
My preferred way is to create a set of library I reuse in all of my project.
I dividet it with different scope for different kind of projects:
MyLib.Core, MyLib.Web, MyLib.Winforms, MyLib.Nhibernate etc etc
I distribute them with nuget so I can easily handle update and versioning

Creating a data driven resource manager while keeping the VS2010 designer features

I'm quite used to consume resx designer generated classes, for a clean coding and to avoid mistakes.
However, in a specific project, I need to rely to a database table, containing all labels and their translations in several languages.
I could simply create my own class and get the translated label, however I'd also like to take advantage of VS2010 resource manager and autogenerated *.Designer.cs classes.
I didn't figure out how to do that.
The *.Designer.cs file contains the following comment:
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
So, I suppose I must use the StronglyTypedResourceBuilder (or my own version of this class).
But how to automatically build the class during the design?
And how to save/retrieve data from the DB?

MvcScaffolding a new project and add it to the solution (Large Scale Generation)

If I want to Large Scale Generation and define
Application = Framework (binary core components) + Generated Code + Custom Code
How would I go about creating code generation framework using scaffolding to generate multiple projects and associated files from some metadata (let's say a DSL model defined in a solution folder)
I know that I can use MvcScaffolding powershell cmdlets to add files to the current on another project.
I need to know if I can add a new project (Class library, Web appication) to the current solution from some kind of project template, apply source transformation and possibly merge some custom data. That would allow additional files to be added and I would prefer that both creation of the project and adding some files initially be done in one powershell line based on some input parameters (let say the name of some DSL model, XSD schema, XML data)
Could I just create a new solution and invoke some scaffolders? Are there scaffolders at a solution level?
I would like to have a scaffodling framework resembling software factories (Web service software factory). Any samples, ideas, articles?
Thanks
Rad
I don't see any reason why not.
Your T4 templates can access EnvDTE and so do all sorts of fun VS automation, and of course the .ps1 powershell scripts can (I guess - I am no powershell guru) do pretty much anything you yourself can do on your box.
But out of interest why would you want to generate whole projects? i.e. are you sure that is time saving?

changes to datacontext and linq-to-sql data model

I'm building an asp application from the ground up using Linq-to-SQL as my ORM. I have a dbml file with a datacontext that includes all the tables of the database (15 for now). If I make changes to the database, by adding a table, adding fields or by changing the data type of a field for instance, how are these kinds of change handled when they occur?
Do I simply drag and drop the new table on the ORM mapper and voila?
Thanks.
Have a look at How to update Linq to SQL dbml file? [best practice] there you will find your answer.
I prefer to use SqlMetal (via a bat file) to fully regenerate the DB schema.
However, you'll find that SqlMetal generates for the entire database. To remove certain tables or relationships; and/or to rename certain tables or generated properties you could look at my blog on filtering items generated by SqlMetal using Powershell.
However, you could of course do it all via the visual studio designer, but that's manual and annoying to repeat for a small change. Or you could manually deal with DBML but that is nasty.
The Visual Studio IDE transfer the changes you do to your dbml file to your ORM classes automatically.

Resources