【问题标题】:ASP.NET 5 with MongoDB带有 MongoDB 的 ASP.NET 5
【发布时间】:2015-02-12 18:11:51
【问题描述】:

试图让 ASP.NET 5 网站与 MongoDB C# 驱动程序集成,但遇到了一些问题。

首先,此处列出的示例http://docs.mongodb.org/ecosystem/drivers/csharp/ 都被标记为已过时。

其次,当我尝试构建时,我遇到了非常奇怪的编译错误(找不到类型或命名空间),即使在 IDE 中看起来一切正常。

这是我非常基本的 HomeController.cs

using Microsoft.Framework.DependencyInjection;
using Microsoft.AspNet.Mvc;
using MongoDB.Driver;
using System;

namespace Docker.Web.Controllers
{
    public class HomeController : Controller
    {
        private AppSettings _appSettings;

        public HomeController(IServiceProvider serviceProvider)
        {
            _appSettings = serviceProvider.GetService<AppSettings>();
        }

        public IActionResult Index()
        {
            var server = new MongoClient(_appSettings.MongoConnection).GetServer();
            var database = server.GetDatabase(_appSettings.MongoDatabase);

            return View();
        }
    }
}

主要问题是我可以在 ASP.NET 5 中使用 C# MongoDB 驱动程序吗?

使用 Visual Studio 2015 预览版并针对 KRE 版本KRE-CoreCLR-x86.1.0.0-beta2

非常感谢任何帮助!

【问题讨论】:

  • 请查看重复的问题,我认为您遇到了同样的问题 - MongoDB 仅适用于桌面 CLR (aspnet50)。基本上,您的代码对于 DesktopCLR 构建良好,而对于 CoreCLR 则失败。
  • 你不能从 coreclr 引用它,除非这个包是用 coreclr 支持构建的。我相信随着 aspnet5 越来越接近 RTM,我们会看到这种情况。但是,您仍然可以将它与 aspnet5 一起使用。

标签: mongodb-.net-driver asp.net-core


【解决方案1】:

您可以将 MongoDB 与 Visual Studio 2015(11 月 10 日更新)一起使用。

从 project.json 中删除 1 行;

"frameworks": {
    "dnx451": { }, // don't forget to remove ','
    "dnxcore50": { } // remove this line
}

现在您可以构建您的解决方案而不会出现任何错误或警告。

【讨论】:

  • 男人想用.NET Core版本,现在不行了。
  • 你的具体问题是什么,你能提供一些细节吗?
  • 我们需要使用 dnxcore50 运行时
  • Asp.net 5 VS 2015:我仍然遇到这个错误(当我使用“MongoDB.Driver.Core”时:“2.2.3”):错误 CS0246 类型或命名空间名称“MongoDB”找不到(您是否缺少 using 指令或程序集引用?) Core 5.0 ...cs 1 Active 和 BsonDocument 相同。尝试使用 "MongoDB.Bson": "2.2.3","MongoDB.Driver": "2.2.3" 但同样的问题,错误 NU1002 项目中的依赖项 MongoDB.Driver 2.2.3 不支持框架 DNXCore,Version=v5 .0.
  • 好消息,看来 .NET Core 应该在 MongoDB 2.3 according to their JIRA 中得到支持。
【解决方案2】:

CoreCLR 不支持 C# 驱动程序,但在完整的 CLR451 模式下支持。

我使用VS2015 CTP创建了一个示例项目

项目.json

{
    "version": "1.0.0-*",
    "dependencies": {
        "mongocsharpdriver": "1.10.0.0"
    },

    "frameworks": {
        "aspnet50": {
            "dependencies": {
            }
        }
    }
}

代码

using System;
using System.Linq;
using MongoDB.Driver.Linq;
namespace MongoDBvNext
{
    public class Class1
    {
        public Class1()
        {
            var client = new MongoDB.Driver.MongoClient("");
            var server = client.GetServer();
            var db = server.GetDatabase("samples");
            var samples = db.GetCollection<Sample>("samples");
            samples.Insert(new Sample { Name = "sample" });
            var sample = samples.AsQueryable<Sample>().Where(x => x.Name == "Sample").FirstOrDefault();
            if (sample == null)
                Console.WriteLine("id: {0} name: {1} ", sample.Id.ToString(), sample.Name);
            else
                Console.WriteLine("Data does not exists");
        }

        public class Sample
        {
            public string Name { get; set; }
            public MongoDB.Bson.ObjectId Id { get; set; }
        }
    }
}

【讨论】:

  • 我也在寻找同样的答案,谢谢儿子!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
相关资源
最近更新 更多