microsoft / DACExtensions

DACExtensions contains samples that extend Data-Tier Applications using DacFx. These samples include deployment contributors and static code analysis rules that can be used with Visual Studio as well as examples of how to use the DacFx public mode
MIT License
125 stars 41 forks source link

DacFx .NET Core generate .dacpac without Origin.xml #32

Closed lucas-mv closed 5 years ago

lucas-mv commented 5 years ago

I managed to generate a .dacpac file programmatically from a model on Ubuntu 18.04 with .NET core 2.2 but the file came without the Origin.xml file, am I doing something wrong?

using System;
using System.IO;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlServer.Dac;

namespace dacpacPOC
{
    class Program
    {
        static void Main(string[] args)
        {
            var script = "CREATE SCHEMA [Test]\r\nGO\r\n" +
                "CREATE TABLE [Test].[Identity]([TestId] int NOT NULL IDENTITY,\r\n" +
                "CONSTRAINT [PK_Identity] PRIMARY KEY ([TestId]))\r\nGO\r\n";
            var tSqlModelOptions = new TSqlModelOptions
            {
                AnsiNullsOn = true,
                Collation = "Latin1_General_CI_AI",
                CompatibilityLevel = 130,
                QuotedIdentifierOn = true
            };
            var outputFilename = "Identity.dacpac";
            using (var tSqlModel = new TSqlModel(SqlServerVersion.Sql130, tSqlModelOptions))
            {
                tSqlModel.AddObjects(script);
                if (File.Exists(outputFilename))
                {
                    File.Delete(outputFilename);
                }
                DacPackageExtensions.BuildPackage(outputFilename, tSqlModel, new PackageMetadata { Name = outputFilename }, new PackageOptions { });
            }
        }
    }
}

My .csproj file is the following:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.SqlServer.DACFx" Version="150.4384.2-preview" />
  </ItemGroup>

</Project>
pensivebrian commented 5 years ago

There was a bug on .NET Core where writing to a .bacpac file would throw an exception, so the origin.xml file would not be written. This is fixed in the latest nuget packages. I think the above code missed the exception since it's not using a try{} catch{} block to log failures in the catch.