这就是我解决这个问题的方法,以防其他人发现这很有用......
策略是在构建数据库项目之前设置一个 sqlcmdvars 变量。此变量将包含可以从部署后脚本引用的构建文件夹的绝对路径。然后,在部署脚本中将它用于您可能需要的任何其他文件或资源将是一件简单的事情。这种策略的优点是所有路径都是相对于项目文件的,而不需要硬编码的共享路径。
创建一个新的 Sql 命令变量名称 $(MSBuildProjectDirectory)。这将在预构建脚本中被覆盖。
创建一个将设置 sql 命令变量并构建数据库的 msbuild 脚本。
<Project ToolsVersion="4.0" DefaultTargets="BuildDatabase" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<PropertyGroup>
<DatabaseServer>(Local)</DatabaseServer>
<DeploymentConnectionString>Data Source=$(DatabaseServer)%3BIntegrated Security=True%3BPooling=False</DeploymentConnectionString>
<Configuration>Release</Configuration>
</PropertyGroup>
<Target Name="BuildDatabase">
<!-- Sets the projet path variable so that the post deployment script can determine the location of the bulk insert csv files. -->
<XmlUpdate
Prefix="urn"
Namespace="urn:Microsoft.VisualStudio.Data.Schema.Package.SqlCmdVars"
XmlFileName="$(MSBuildProjectDirectory)\DatabaseProjectName\Properties\Database.sqlcmdvars"
XPath="/urn:SqlCommandVariables/urn:Properties/urn:Property[urn:PropertyName='MSBuildProjectDirectory']/urn:PropertyValue"
Value="$(MSBuildProjectDirectory)\DatabaseProjectName" />
<MSBuild
Projects="DatabaseProjectName\DatabaseProjectName.dbproj"
Properties="Configuration=$(Configuration);
TargetDatabase=DatabaseName;
TargetConnectionString=$(DeploymentConnectionString);
GenerateDropsIfNotInProject=True;
BlockIncrementalDeploymentIfDataLoss=False;
DeployToDatabase=True;
IgnorePermissions=True"
Targets="Build;Deploy">
<Output TaskParameter="TargetOutputs" ItemName="SqlFiles"/>
</MSBuild>
</Target>
如下更新您的部署后脚本...
BULK INSERT [dbo].[TableName] FROM '$(MSBuildProjectDirectory)\Scripts\Post-Deployment\Data\YourDataFile.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR='\n')