带有MongoDB的ASP.NET 5 [英] ASP.NET 5 with MongoDB
问题描述
试图将ASP.NET 5网站与MongoDB C#驱动程序集成在一起,但遇到一些问题.
Trying to get an ASP.NET 5 website integrated with the MongoDB C# driver but running into a few issues.
首先,此处列出的示例 http://docs.mongodb.org/ecosystem/drivers/csharp/全部标记为过时.
First of all, the examples listed here http://docs.mongodb.org/ecosystem/drivers/csharp/ are all flagged as obsolete.
其次,即使在IDE中一切正常,我在尝试构建时也遇到了奇怪的编译错误(找不到类型或名称空间).
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.
这是我非常基本的HomeController.cs
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();
}
}
}
主要问题是我可以在ASP.NET 5中使用C#MongoDB驱动程序吗?
Main question is can I use the C# MongoDB driver with ASP.NET 5?
使用Visual Studio 2015预览并定位KRE版本 KRE-CoreCLR-x86.1.0.0-beta2
Using Visual Studio 2015 preview and targeting KRE version KRE-CoreCLR-x86.1.0.0-beta2
任何帮助将不胜感激!
推荐答案
CoreCLR不支持C#驱动程序,但在完全CLR451模式下受支持.
The C# Driver is not supported in CoreCLR but is supported in full CLR451 mode.
我使用VS2015 CTP创建了一个示例项目
I created a sample project using VS2015 CTP
project.json
project.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; }
}
}
}
这篇关于带有MongoDB的ASP.NET 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!