ASP.NET 5 with MongoDB

最后都变了- 提交于 2019-11-30 23:41:36

问题


Trying to get an ASP.NET 5 website integrated with the MongoDB C# driver but running into a few issues.

First of all, the examples listed here http://docs.mongodb.org/ecosystem/drivers/csharp/ are all flagged as obsolete.

Secondly, I'm getting really weird compile errors (type or namespace could not be found) when I try and build even though everything looks OK in the IDE.

Here's my very basic 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();
        }
    }
}

Main question is can I use the C# MongoDB driver with ASP.NET 5?

Using Visual Studio 2015 preview and targeting KRE version KRE-CoreCLR-x86.1.0.0-beta2

Any help is greatly appreciated!


回答1:


The C# Driver is not supported in CoreCLR but is supported in full CLR451 mode.

I created a sample project using VS2015 CTP

project.json

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

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

Code

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; }
        }
    }
}



回答2:


You can use MongoDB with visual studio 2015 (Updated 10 November).

remove 1 line from project.json;

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

now you can build your solution without any error or warning.



来源:https://stackoverflow.com/questions/28484761/asp-net-5-with-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!