ASP.NET 5 with MongoDB

≯℡__Kan透↙ 提交于 2019-12-01 03:23:22
Son_of_Sam

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

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.

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