问题
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.
回答1:
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"
}
}
}
回答2:
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 :) !
来源:https://stackoverflow.com/questions/40558718/how-to-access-nuget-repository-from-net-core-using-visual-studio-code