无法在.Net Core API中连接到Mongo DB

一直在使用.Net核心networking应用程序,现在我想连接Mongo与在.Net核心上编写.Net C#的api。 但是,一切都在Docker里面。 这就是说,每个元素都在工作。 我可以从命令行编辑数据库,所以我知道它的启动和运行。 另外,我应该注意到我在最新的Ubuntu上。

在.csproj文件中,我添加了Mongo驱动程序:

<PackageReference Include="MongoDB.Driver.Core" Version="2.3.0" /> <PackageReference Include="MongoDB.Bson" Version="2.3.0" /> 

然后在program.cs文件中添加:

 using MongoDB.Driver; // at top using MongoDB.Bson; ... ... var mongo = MongoClient("mongodb://localhost:27017"); // in main method 

这是我使用docker构build应用程序时得到的错误:

 Program.cs(13,25): error CS0103: The name 'MongoClient' does not exist in the current context [/src/src/Identity.api/Identity.api.csproj] 

我添加了正确的驱动程序,他们是否在错误的位置?

让我知道是否需要任何额外的信息。 非常感谢,我期待能够连接到数据库!

MongoDB.Driver.MongoClienttypes包含在MongoDB.Driver包中,所以你需要引用MongoDB.Driver.Core是不够的。

MongoDB.Driver.Core包含MongoDB .Net驱动程序的“核心组件”,而不是驱动程序的.Net Core版本。