Question

I have defined a Visual Studio template called classDB.cs. I would like the default name for the class to appear as [projectname]DB.cs, where [projectname] is the name of the current project (as entered in the Create Project dialog). Is there a way to achieve this? I tried setting the name of the class to $safeprojectname$DB.cs, but that didn't work.

UPDATE

I modified my project template but give's this error when it's generating the project

enter image description here

here's the template class

namespace $safeprojectname$.Models
{
    public class $safeprojectname$DB : DbContext
    {

    }
}

the class in the project

Was it helpful?

Solution

I have been battling with a similar error to this for days, and I finally figured it out. Visual Studio escapes the $ in the .csproj file. So you will have a node that looks like this:

<Compile Include="Models\%24safeprojectname%24DB.cs" />

Open up the .csproj file in a text editor, and change it to:

<Compile Include="Models\$safeprojectname$DB.cs" />

And save the file. Your project will reload, but it won't try to escape the filename again! Export your template, and you should find that the parameter now gets replaced.

OTHER TIPS

Try a template like this:

using System;
//...

namespace $rootnamespace$ {
    class $safeitemname$DB {
    }
}

Works for me.

Make sure you update the correct template (should be located under C:\Users\[user]\Documents\Visual Studio 2010\Templates\ItemTemplates on Windows 7) and restart Visual Studio.


EDIT

The above code is for an Item Template, but that shouldn't differ from a Project Template. According to MSDN, the $safeitemname$ and $safeprojectname$ parameters behaves the same:

safeitemname
The name provided by the user in the Add New Item dialog box, with all unsafe characters and spaces removed.

safeprojectname
The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top