DacFx: Unable to build in VS - Visual Studio support for Microsoft.Build.Sql

  • SqlPackage or DacFx Version: 161
  • .NET Framework (Windows-only) or .NET Core: Windows
  • Environment (local platform and source/target platforms):

Steps to Reproduce:

  1. Attempt to load/build a new sqlproj based on template 0.1.7
  2. Load fails
  3. adding this enables the build:
    <TargetDatabaseSet>True</TargetDatabaseSet>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <TargetFrameworkProfile />
    <ProjectGuid>{f1ee546e-2489-4a7b-9d69-7e2849143721}</ProjectGuid>

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 18
  • Comments: 22 (8 by maintainers)

Most upvoted comments

Preview support for the Microsoft.Build.Sql SDK is landing in Visual Studio soon - will update ya’ll as soon as the first release goes out the door!

Support for Microsoft.Build.Sql sqlprojects is planned for future Visual Studio (SSDT) releases. Reopening this item until it is shipped

With Visual Studio 17.12 preview 2 - support for Microsoft.Build.Sql SDK-style projects is now available in preview for Visual Studio.

2 key limitations to note:

  • in preview 2 the file extension for the project is .sqlprojx instead of .sqlproj
  • to use side-by-side with the original SQL projects in Visual Studio, install separate instances of VS on your machine

blog: https://techcommunity.microsoft.com/t5/azure-sql-blog/preview-release-of-sdk-style-sql-projects-in-visual-studio-2022/ba-p/4240616

docs: https://aka.ms/ssdt-sdk-preview

Sharing what my customized .sqproj file ended up being for others who come across this thread while trying to troubleshoot new use of this project sdk:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build">
  <Sdk Name="Microsoft.Build.Sql" Version="0.1.15-preview" />
  <PropertyGroup>
    <Name>YourProjectName</Name>
    <DSP>Microsoft.Data.Tools.Schema.Sql.Sql160DatabaseSchemaProvider</DSP>
    <ModelCollation>1033, CI</ModelCollation>
    <ProjectGuid>{your-project-guild}</ProjectGuid>
    <TargetDatabaseSet>True</TargetDatabaseSet>
    <!-- 
      The sdk will target netstandard2.1 automatically when doing a dotnet cli build but we need to target 
      netframework when the msbuild type is full (a.k.a. building from within Visual Studio) until the Visual Studio
      SSDT extension supports the new sql project SDK.
    -->
    <TargetFrameworkVersion Condition="'$(MSBuildRuntimeType)' != 'Core'">v4.8</TargetFrameworkVersion>
    <TargetFrameworkProfile Condition="'$(MSBuildRuntimeType)' != 'Core'" />
  </PropertyGroup>

  <!-- 
    NOTE: The sql project sdk automatically includes a default globbing pattern for sql files but we still need them to be listed 
    explicitly for VisualStudio SSDT to display them

    See: https://github.com/microsoft/DacFx/blob/main/src/Microsoft.Build.Sql/docs/Converting-Existing.md#default-sql-files-included-in-build
    See: https://github.com/microsoft/DacFx/blob/a888164f1e83f0b64f130875600d90205adb7c23/src/Microsoft.Build.Sql/sdk/Sdk.props#L35
  -->
  <ItemGroup>
    <Build Include="**/*.sql" />
    <Build Remove="bin/**/*.sql" />
    <Build Remove="obj/**/*.sql" />
    <!-- Also Build Remove any .sql file patterns you want to exclude   e.g. -->
    <Build Remove="StaticDataScriptsReferencedInPostDeploy/**/*.sql" />

    <!-- Explicitly include your folders so that VisualStudio SSDT will display your files -->
    <Folder Include="OtherFolder" />
    <Folder Include="StaticDataScriptsReferencedInPostDeploy" />
    <Folder Include="Tables" />

    <None Include="README.md" />
    <None Include="StaticDataScriptsReferencedInPostDeploy/**/*.sql" />
  </ItemGroup>

  <ItemGroup>
    <PostDeploy Include="Script.PostDeployment.sql" />
  </ItemGroup>

  <!-- 
    Building through visual studio (targeting net48) will fail if a previous build through the dotnet core cli
    has run and created the obj/project.assets.json file. This custom target should delete
    the file before the visual studio build starts.
  -->
  <Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(MSBuildRuntimeType)' != 'Core'">
    <Message Importance="high" Text="Ensuring the $(MSBuildThisFileDirectory)\obj\project.assets.json file is removed, if necessary, so that the database project can be built through VisualStudio SSDT without errors" />
    <Delete Files="$(MSBuildThisFileDirectory)\obj\project.assets.json" />
  </Target>

</Project>

When will have support for SDK-style projects in Visual Studio? It seems like an easy-to-do feature that would be very useful for many of us, but seems to be delayed for more than a year already.

I had a similar issue, and I was able to resolve this by deleting the obj folder for my project. And so, I set up the project to clean the project and to delete the obj folder before clean. It’s still a work in progress, but I landed on the code below to both build in Visual Studio 2022 and via the dotnet cli. The dotnet cli seems to build just fine without deleting the obj folder, but it wasn’t deleting the folder, so I just added ContinueOnError=“True”.

Edited code since Visual Studio 2022 didn’t seem to output the dacpac.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0">
  <Sdk Name="Microsoft.Build.Sql" Version="0.1.7-preview" />
  <PropertyGroup>
    <Name>sample</Name>
    <DSP>Microsoft.Data.Tools.Schema.Sql.Sql160DatabaseSchemaProvider</DSP>
    <ModelCollation>1033, CI</ModelCollation>
    <ProjectGuid>{939b2fc6-e398-4920-a5d4-7b3816c0b3a5}</ProjectGuid>
    <TargetDatabaseSet>True</TargetDatabaseSet>
  </PropertyGroup>
  <ItemGroup>
    <Folder Include="Properties" />
  </ItemGroup>
  <Target Name="PreBuild" BeforeTargets="Build">
    <Message Text="PreBuild - Starting to remove obj folder" />
    <!-- Remove obj folder -->
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" Condition="Exists('$(BaseIntermediateOutputPath)')" ContinueOnError="True" />
    <Message Text="PreBuild - Finished removing obj folder" />
  </Target>
</Project>

I had a typo in my previous comment and edited. We do have a future enhancement for the VS to support SDK style projects and will support that in future.

Working example for 1.19-preview with vs2022 17.10.2 (Community Edition)

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build">
  <Sdk Name="Microsoft.Build.Sql" Version="0.1.19-preview" />
  <PropertyGroup>
    <Name>MySample</Name>
    <ProjectGuid>{71b1081a-ce12-4fc4-9c9c-53bf630fc0d7}</ProjectGuid>
    <DSP>Microsoft.Data.Tools.Schema.Sql.Sql160DatabaseSchemaProvider</DSP>
    <ModelCollation>1033, CI</ModelCollation>
    <TargetDatabaseSet>True</TargetDatabaseSet>
  </PropertyGroup>
  <Target Name="BeforeBuild">
    <Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" />
  </Target>
  
  <!-- 
   Support Visual Studio.
   Solution is a mix of example from @RyanThomas73, and a recent commit to DaxFx with support for Visual Studio:
    * https://github.com/microsoft/DacFx/issues/180#issuecomment-2010033767 
    * https://github.com/microsoft/DacFx/commit/3cd56f7c173457ed52e76979dddf901e7285f090
  -->
  <!-- building in Visual Studio requires some sort of TargetFrameworkVersion. So we condition to NetCoreBuild as false to avoid failures -->
  <PropertyGroup Condition="'$(MSBuildRuntimeType)' != 'Core'">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <TargetFrameworkMoniker>.NETFramework,Version=$(TargetFrameworkVersion)</TargetFrameworkMoniker>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
  </PropertyGroup>
  <ItemGroup Condition="'$(MSBuildRuntimeType)' != 'Core'">
    <!-- Build default globbing pattern includes all sql files in all subfolders, excluding bin and obj, visual studio is likely to mess with this... -->
    <Build Include="**/*.sql" />
    <Build Remove="bin/**/*.sql" />
    <Build Remove="obj/**/*.sql" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Stored Procedures\" />
    <Folder Include="Tables\" />
  </ItemGroup>
  <!-- 
    Building through visual studio will fail with `Your project does not reference ".NETFramework,Version=v4.7.2" framework.` if a previous build through the dotnet core cli
    has run and created the obj/project.assets.json file. This custom target will delete the file before the visual studio build starts.
  -->
  <Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(MSBuildRuntimeType)' != 'Core'">
    <Message Importance="high" Text="Ensuring the $(MSBuildThisFileDirectory)\obj\project.assets.json file is removed, if necessary, so that the database project can be built through VisualStudio SSDT without errors" />
    <Delete Files="$(MSBuildThisFileDirectory)\obj\project.assets.json" />
  </Target>
</Project>

When I try to build in visual studio after building in AzureDataSudio I get this error: “Error: : Your project does not reference “.NETFramework,Version=v4.8.1” framework. Add a reference to “.NETFramework,Version=v4.8” in the “TargetFrameworks” property of your project file and then re-run NuGet restore.”

If I delete the project.assets.json file from the obj folder then it will build fine.

Found there is this already: <Target Name="BeforeBuild"> <Delete Files="$(BaseIntermediateOutputPath)\project.assets.json" /> </Target>

But it doesn’t work for me had to switch to: <Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Delete Files="$(SolutionDir)$(ProjectName)\obj\project.assets.json" /> </Target>

I used just $(ProjectDir) to start with - this builds in visual studio but failed more often than not in Azure Data Studio with "C:\Program Files\dotnet\sdk\7.0.400\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(321,5): error MSB4018: The “GetProjectReferencesFromAssetsFileTask” task failed unexpectedly. "

Using the SolutionDir instead makes the path *Undefined* in AzureDataStudio

Yes, as the title of the issue states this is about building in VS. Its all fine that you can build in limited cross platform tooling but the circle is not complete until you can build in VS and get the intellisense and table editing experience.

@Real-JD-UK you are not using the latest SDK version