简体   繁体   中英

How to deploy dacpac file from a nuget package in c#

I have a nuget package containing my SQL database project. How can I reference the dacpac file in the package in order to deploy it to my azure sql server? I would like to do this from my ASP.net core application.

I needed to do this too, so here is what worked for me:

If your application uses the new SDK format, you can reference content files in a referenced nuget package in your project. You need to add the <GeneratePathProperty> node with a value of true in the package reference:

  <ItemGroup>
    <PackageReference Include="My.Package" Version="1.0.1">
      <GeneratePathProperty>true</GeneratePathProperty>
    </PackageReference>
  </ItemGroup>

Then you can include the file you want to reference using a content link. The VS variable name is by convention starting with the prefix "Pkg" and replacing periods with underscores. You can also optionally include a <Link> node which provides a "notional" path for the included file when viewing it in your project. Example below:

  <ItemGroup>
    <Content Include="$(PkgMy_Package)\contentFiles\any\net48\SomeDb.dacpac">
      <Link>dacpac\SomeDb.dacpac</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Pack>true</Pack>
    </Content>
  </ItemGroup>

The content file will be copied to the build output folder during build just like any other content file.

This is supported from VS 2017 15.9. Credit to Georg Dangl from his blog post: https://blog.dangl.me/archive/accessing-nuget-package-paths-in-your-net-sdk-based-csproj-files/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM