Visual Studio项目模板:用项目名称替换 [英] Visual Studio item template: Replace with project name

查看:143
本文介绍了Visual Studio项目模板:用项目名称替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual Studio 2013中为EF Code-First上下文类创建项目模板.除了我想要一个指向当前项目中的命名空间的using语句外,所有这些都可以正常工作.例如,在 ProjectName 中创建模板项的实例时,上下文的using语句将是:

I'm trying to create an Item Template in Visual Studio 2013 for an EF Code-First context class. I have it all working except I want a using statement pointing to a namespace within the current project. For example, when creating an instance of the template item within ProjectName.Contexts the using statement would be:

使用 ProjectName .Models

using ProjectName.Models

问题是我找不到替代项目模板中零件的方法.据我所知,$ projectname $和$ safeprojectname $仅适用于项目模板,不适用于项目模板.

The problem is that I can't find a way to substitute the part in an item template. As near as I can determine, $projectname$ and $safeprojectname$ only work in project templates, not item templates.

我的模板如下:

using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using $safeprojectname$.DAL.Models; // This is the problem line

namespace $rootnamespace$
{
    public class $safeitemrootname$
        : DbContext
    {
        // Constructors =======================================================
        public $safeitemrootname$()
            : base("$safeitemrootname$")
        {
        }

        // DBSets =============================================================
        // public DbSet<Company> Companies { get; set; }

        // Configuration ======================================================
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

更新:

为回应Hans的评论,这是我尝试使用$ projectname $时的输出($ safeprojectname $的用法相同):

In response to Hans comment, here's the output when I try to use $projectname$ ($safeprojectname$ does the same):

using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using $projectname$.DAL.Models; // This is the problem line

如您所见,$ projectname $替换只是被忽略.

As you can see, the $projectname$ substitution just gets ignored.

任何帮助,我们都会感激的,

Any help would be appreciated,

Jason

推荐答案

如以下MSDN链接中所述:

As mentioned in the below MSDN link:

https://msdn.microsoft.com/en-us/library/eehb4faa.aspx

仅当在新建项目"对话框中提到项目名称时,项目名称才会出现.因此,如果您在现有项目中使用它,它将无法正常工作.请参见下面的屏幕截图:

The project name will come only when you have mentioned project name in New Project dialog box. So, if you are using it in existing project, it won't work. See screenshot below:

这篇关于Visual Studio项目模板:用项目名称替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆