【问题标题】:How to load VS Database Project from .NET Core app?如何从 .NET Core 应用程序加载 VS 数据库项目?
【发布时间】:2020-06-21 16:19:27
【问题描述】:

默认情况下,.sqlproj 文件中有一个带有此类导入行的 Visual Studio SQL Server 数据库项目:

<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" />

虽然 VisualStudioVersion 11.0 没有包含 SSDT 的目录,但它可以从 Visual Studio 正确打开。但是,当尝试使用 Microsoft.Build 包从 .NET Core 应用程序加载项目时,会引发错误:

找不到导入的项目“D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTask​​s.targets”。确认导入声明“D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTask​​s.targets”中的表达式是正确的,并且该文件存在于磁盘上。
D:\Database1\Database1\Database1.sqlproj

项目加载代码如下所示

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

【问题讨论】:

  • 终极目标是什么?您可以使用忽略导入错误的项目。
  • @UserName 我想要的只是使用 AddItem 方法将一些项目添加到项目中,然后保存它。我还寻找了一个忽略导入错误的解决方案,但只发现 SkipEvaluation 属性没有帮助。
  • 答案解决了你的问题吗?
  • @UserName 是的,谢谢。

标签: c# .net-core msbuild sqlproj


【解决方案1】:

我只想使用 AddItem 方法将一些项目添加到项目中 然后保存。

正如我所说,您可以忽略错过的导入,并且我相信这将防止错误发生,为此您可以使用 Project 构造函数的以下重载:

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

loadSettings 设置为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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多