简体   繁体   中英

How to load VS Database Project from .NET Core app?

There is an Visual Studio SQL Server Database Project with such import lines inside.sqlproj file by default:

<PropertyGroup>
  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
  <!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
  <SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
  <VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />

Although there are no directories with SSDT for VisualStudioVersion 11.0, it opens correctly from Visual Studio. But when trying to load the project from .NET Core application using Microsoft.Build package it throws an error:

The imported project "D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found. Confirm that the expression in the Import declaration "D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" is correct, and that the file exists on disk.
D:\Database1\Database1\Database1.sqlproj

The code for project loading looks like

var project = new Microsoft.Build.Evaluation.Project(@"D:\Database1\Database1\Database1.sqlproj");

All I want is to add some items into the project using AddItem method and then Save it.

As I said, you can ignore missed imports, and I believe that this will prevent errors from occurring, for this you can use the following overload of the Project constructor:

public Project (string projectFile, System.Collections.Generic.IDictionary<string,string> globalProperties, string toolsVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings);

Set loadSettings to IgnoreMissingImports :

/// <summary>
/// Flags for controlling the project load.
/// </summary>
/// <remarks>
/// This is a "flags" enum, allowing future settings to be added
/// in an additive, non breaking fashion.
/// </remarks>
[Flags]
[SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue", Justification = "Public API.  'Default' is roughly equivalent to 'None'. ")]
public enum ProjectLoadSettings
{
    /// <summary>
    /// Normal load. This is the default.
    /// </summary>
    Default = 0,

    /// <summary>
    /// Ignore nonexistent targets files when evaluating the project
    /// </summary>
    IgnoreMissingImports = 1,

    /// <summary>
    /// Record imports including duplicate, but not circular, imports on the ImportsIncludingDuplicates property
    /// </summary>
    RecordDuplicateButNotCircularImports = 2,

    /// <summary>
    /// Throw an exception and stop the evaluation of a project if any circular imports are detected
    /// </summary>
    RejectCircularImports = 4,

    /// <summary>
    /// Record the item elements that got evaluated
    /// </summary>
    RecordEvaluatedItemElements = 8,

    /// <summary>
    /// Ignore empty targets files when evaluating the project
    /// </summary>
    IgnoreEmptyImports = 16,

    /// <summary>
    /// By default, evaluations performed via <see cref="Project"/> evaluate and collect elements whose conditions were false (e.g. <see cref="Project.ItemsIgnoringCondition"/>).
    /// This flag turns off this behaviour. <see cref="Project"/> members that collect such elements will throw when accessed.
    /// </summary>
    DoNotEvaluateElementsWithFalseCondition = 32,

    /// <summary>
    /// Ignore invalid target files when evaluating the project
    /// </summary>
    IgnoreInvalidImports = 64,

    /// <summary>
    /// Whether to profile the evaluation
    /// </summary>
    ProfileEvaluation = 128,
}

ProjectLoadSettings Enum | ProjectLoadSettings Enum [Github]

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