我已经为 MVC3 编写了一个自定义脚手架,使用 T4 模板为作为参数传递给脚手架的数据库和表的删除存储过程搭建脚手架。当我运行脚手架时,会在正确的位置创建输出文件,但出现以下错误:
Invoke-ScaffoldTemplate:无法添加“UpdateCustomerCoupon.sql”。已存在具有该名称的文件。在 line:1 char:23 + param($c, $a) return 。<<<< $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 选项调用脚手架,如下所示:“ Scaffold DeleteSQL -DatabaseName $DatabaseName -TableName $TableName -Force ”。我也使用了“ -Force true ”和“ -Force:true ”,但没有成功。还要注意 $Force 参数无论如何都设置为 true,所以我认为它应该默认覆盖,对吧?
我需要做什么才能摆脱这个错误?
谢谢您的帮助。