简体   繁体   English

如何从 .NET Core 应用程序加载 VS 数据库项目?

[英]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:默认情况下,Visual Studio SQL 服务器数据库项目在 .sqlproj 文件中包含这样的导入行:

<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.尽管 VisualStudioVersion 11.0 没有包含 SSDT 的目录,但它可以从 Visual Studio 正确打开。 But when trying to load the project from .NET Core application using Microsoft.Build package it throws an error:但是,当尝试使用 Microsoft.Build package 从 .NET 核心应用程序加载项目时,它会引发错误:

The imported project "D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" was not found.未找到导入的项目“D:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets”。 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:\TestApp\TestApp\bin\Debug\netcoreapp3.1\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets”中的表达式是正确的,并且该文件存在于磁盘上。
D:\Database1\Database1\Database1.sqlproj 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.我想要的只是使用 AddItem 方法将一些项目添加到项目中,然后保存它。

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

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 :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 枚举| ProjectLoadSettings Enum [Github] ProjectLoadSettings 枚举[Github]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在VS2010和VS2012中加载数据库项目? - How to load database project in VS2010 and VS2012? 如何在 VS Code 中将 .net 核心项目的目标框架从 netstandard2.1 更改为 netcoreapp3.1 - How to change target framework of a .net core project from netstandard2.1 to netcoreapp3.1 in VS Code 如何将 .Net Core 中的内置程序集添加到 VS Code 中的 C# 项目 - How to add built-in assembly from .Net Core to a C# project in VS Code 如何在.NET Core中从流加载程序集 - How to load assembly from stream in .NET Core 在控制台应用程序中重用来自我的 ASP.NET Core MVC 应用程序的代码:如何进行数据库连接 - Reusing code from my ASP.NET Core MVC app in console app : how to do database connection 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core 如何从 VS2019 中的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储 - How to connect to a Azure File Store from a .NET Core 3.1 console app in VS2019 如何将Blazor应用程序从.Net Core 3降级到.Net Core 2 - How to downgrade Blazor app from .Net Core 3 to .Net Core 2 NET Core 2.0在vs2017中如何创建控制台应用程序? - how to create a console app in vs2017 with .net core 2.0? 如何加载位于 .NET Core 控制台应用程序文件夹中的程序集 - How to load assemblies located in a folder in .NET Core console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM