我使用T4模板为MVC3编写了一个自定义脚手架来为数据库和作为参数传递给脚手架的表构建一个删除存储过程。当我运行脚手架时,输出文件是在正确的位置创建的,但是我收到以下错误:
Invoke-ScaffoldTemplate:无法添加“UpdateCustomerCoupon.sql”。一个 具有该名称的文件已存在。在行:1 char:23 + param($ c,$ a)返回。 <<<< $ c @a + CategoryInfo:NotSpecified:(:) [Invoke-ScaffoldTemplate],COMException + FullyQualifiedErrorId:T4Scaffolding.Cmdlets.InvokeScaffoldTemplateCmdlet
文件是否存在于输出位置并不重要。我每次都会收到这个错误。
以下是脚手架的PowerShell脚本:
[T4Scaffolding.Scaffolder(Description = "Enter a description of DeleteSQL here")][CmdletBinding()]
param(
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$DatabaseName,
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$TableName,
[string]$Project,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $true
)
$outputPath = "Scripts/SQL/$TableName/Delete$TableName"
Add-ProjectItemViaTemplate $outputPath -Template DeleteSQLTemplate `
-Model @{ TableName = $TableName; DatabaseName = $DatabaseName; Project = $Project } `
-SuccessMessage "Added DeleteSQL output at {0}" `
-TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force
Write-Host "Scaffolded DeleteSQL"
这是T4模板代码:
<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" Debug="True" #>
<#@ Output Extension="sql" #>
<#@ assembly name="System.Collections" #>
<#@ assembly name="System.Configuration" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.Web" #>
USE [<#= Model.DatabaseName #>]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Delete<#= Model.TableName #>] (
@P<#= Model.TableName #>ID AS BIGINT,
@PChangedByUserID BIGINT,
@PChangedByURL VARCHAR(1024),
@PChangedByIpAddress varchar(16)
)
AS
SET NOCOUNT ON
BEGIN TRY
BEGIN TRANSACTION
-- update user history
DECLARE @userHistoryID BIGINT = 0;
DECLARE @details VARCHAR(4096) = '[<#= Model.TableName #>] ID ''' + CAST(@P<#= Model.TableName #>ID AS VARCHAR) + ''' was deleted.'
EXEC InsertUserHistory @PChangedByUserID, @details, @PChangedByURL, @PChangedByIpAddress, @userHistoryID OUTPUT
-- Rollback transaction if user history was not created
IF(@userHistoryID = 0) BEGIN
ROLLBACK
SELECT CAST(-1 AS INT)
RETURN
END
DELETE FROM
[<#= Model.TableName #>]
WHERE
[ID] = @P<#= Model.TableName #>ID
COMMIT
SELECT CAST(1 AS INT)
END TRY
BEGIN CATCH
ROLLBACK
SELECT CAST(-2 AS INT)
END CATCH
RETURN
我通过在程序包管理器控制台中键入“ Scaffold DeleteSQL -DatabaseName $ DatabaseName -TableName $ TableName ”来调用脚手架。
我也尝试使用-Force选项调用脚手架,如下所示:“脚手架DeleteSQL -DatabaseName $ DatabaseName -TableName $ TableName -Force ”。我还使用了“ -Force true ”和“ -Force:true ”而没有运气。另请注意,$ Force参数无论如何都设置为true,所以我认为它应该默认覆盖,对吧?
我需要做些什么才能摆脱这个错误?
感谢您的帮助。
答案 0 :(得分:4)
看起来问题与输出生成的路径有关。
我在PowerShell脚本中更改了这一行:
$outputPath = "Scripts/SQL/$TableName/Delete$TableName"
到此:
$outputPath = "Scripts\SQL\$TableName\Delete$TableName"
我唯一改变的是从“/”到“\”的斜杠。现在,我不再收到错误消息了。