简体   繁体   中英

How to access Nuget repository from .NET Core using Visual Studio Code

I'm working on a .NET Core project with a friend and we're splitting the work as we see best. We're currently trying to configure a database application to communicate with so we need a database "driver" library.

I've decided on MongoDB, and I've found this page which seems to suggest that the following code will configure our project somehow to use the library. Selecting .NET Core and Nuget (both of which I've installed locally and on our server), I get the following code which seems to be the way to add the library:

<packages>
    <package id="MongoDB.Driver.Core" version="2.3.0" />
    <package id="MongoDB.Bson" version="2.3.0" />
</packages>

The problem is, I've no idea how nor where to use this. The configuration files I'm looking at are almost entirely json. I get the impression this was actually designed to be used with Visual Studio since its project files still use XML.

So the question: How do I import functions from an external library for .NET Core development using Visual Studio Code? I suspect I just need to translate this into a suitable format (JSON) and that'll do, but I'm not sure what it's going to look like nor which file it's supposed to live in.

I've tried to do this by opening the quick-menu, entering selecting >Nuget: Add Package, then finding and selecting the package MongoDB.Driver.Core, but this results in the error message:

Could not read project.json, please try again.

I don't entirely understand this, I suspect it may be that project.json is required at the root level of the project, which it is not in my case, but this doesn't entirely make sense.

I've tried creating a new project, it looks like there is a bug that Code expects project.json to be at the root of the open folder (which it is not). The end solution for me was to add "MongoDB.Driver.Core": "2.3.0" to the project.json file, so it now looks like so (this is a default project.json with the added bit):

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "MongoDB.Driver.Core": "2.3.0"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Update:

Right now before .NET Core 2.0 I use in Visual Studio Code Nuget Package Extension. You can download this extension via Extension tools in VSC or from URL:

https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager

To use this extension use CTRL + SHIFT + P and then type Nuget Package Manager: Add Package and then download what you want :) !

这是一篇旧帖子,但想分享一下,因为我一直在寻找相同的内容:由于您已经有一个正在使用 VSCode 的 .NET 项目,因此您可以使用“dotnet”命令,如下所示:

dotnet add package MongoDB.Driver --version 2.11.6

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