How to include created database project created by dacpac in cs project

I have a separate database project that Id would like to build in one solution and then reference the created dacpac. When I try to add a database project, it builds fine, and the dll is added to the secondary project file, but dacpac is not.

Is there a way that dacpac can be copied to my main project via msbuild? I keep thinking that there must be a way to modify the sqlproj file or the csproj file so that dacpac is included as one of the project outputs. My knowledge of msbuild is small, Ive been unable to figure it out.

It seems to me that I need to add dacpac to say the element '@ (ReferenceCopyLocalPaths), but I could not understand it. Any advice or suggestions would be appreciated.

I tried to do something a bit like the one mentioned here MSBuild - ItemGroup of all bin directories in subdirectories by doing:

   <Target Name="AfterBuild">
    <Message Text="@(MainAssembly)" />
    <!--<DacPacs Include="%(ProjectReference.Directory)**"  />-->
    <ItemGroup>
      <DacPacs Include="%(ProjectReference.Directory)**/*bin*/*.dac" />
    </ItemGroup>
    <Message Text="@(ReferenceCopyLocalPaths)" />
    <Message Text="DacPacs: @(DacPacs)" />
    <Message Text="Target Database: $(TargetDatabase)" />
  </Target>

which gives nothing for DacPacs (when adding a template). I also tried to reference one of the element groups from the sqlproj file, but it comes out empty:

+3
source share
1 answer

In the project properties, you can add the pre-build event command line to get a copy of the dacpac file.

Prebuild event for dacpac copy

Or you can just add it to csproj:

<PropertyGroup>
  <PreBuildEvent>copy "$(SolutionDir)DatabaseProject\bin\$(ConfigurationName)\DatabaseProject.dacpac" "$(ProjectDir)\DatabaseProject.dacpac"</PreBuildEvent>
</PropertyGroup>

This will only work if the database project was created first, so you should add a dependency. Right-click on the solution and select Project Dependencies..., then select the main project and make sure that it depends on the database project.

enter image description here

0

All Articles