asp.net-mvc-3t4scaffoldingasp.net-mvc-scaffoldingt4scaffolding

MVC3/T4 Custom Scaffolder Gives File Exists Error Even When File Doesn't Exist


I have written a custom scaffolder for MVC3 using T4 templates to scaffold a delete stored procedure for a database and table passed to the scaffolder as parameters. When I run the scaffolder the output file is created in the correct place, but I get the following error:

Invoke-ScaffoldTemplate : Unable to add 'UpdateCustomerCoupon.sql'. A file with that name already exists. At line:1 char:23 + param($c, $a) return . <<<< $c @a + CategoryInfo : NotSpecified: (:) [Invoke-ScaffoldTemplate], COMException + FullyQualifiedErrorId : T4Scaffolding.Cmdlets.InvokeScaffoldTemplateCmdlet

It doesn't matter if the file exists at the output location or not. I get this error every time.

Here is the PowerShell script for the scaffolder:

[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"

And here is the T4 Template code:

<#@ 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

I invoke the scaffolder by typing "Scaffold DeleteSQL -DatabaseName $DatabaseName -TableName $TableName" into the Package Manager Console.

I have also tried invoking the scaffolder with the -Force option like this: "Scaffold DeleteSQL -DatabaseName $DatabaseName -TableName $TableName -Force". I have also used "-Force true" and "-Force:true" with no luck. Also notice that the $Force parameter is set to true anyway, so I think it should overwrite by default, right?

What do I need to do to get rid of this error?

Thanks for the help.


Solution

  • Looks like the problem had to do with the path the output was generate to.

    I changed this line in the PowerShell script:

    $outputPath = "Scripts/SQL/$TableName/Delete$TableName"
    

    to this:

    $outputPath = "Scripts\SQL\$TableName\Delete$TableName"
    

    The only thing I changed was the slashes from "/" to "\". Now, I don't get the error message anymore.