Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 with MongoDB

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!

like image 746
timothyclifford Avatar asked Feb 12 '15 18:02

timothyclifford


People also ask

Can we use MongoDB with ASP NET?

If you want to download the source code, you can do that on our ASP.NET Core and MongoDB repo. To help us build a web API with a schema-less database, we'll look at how to set up an ASP.NET Core application with MongoDB.

Can Entity Framework work with MongoDB?

Short answer - no, it's for sure possible, but not reasonable. MongoDB is document database and not support any physical relations between collections. EF is a good fit for relational databases like SQL, MySQL, etc. MongoDB works faster with embedded documents.

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

How to get started with MongoDB ASP NET?

Getting Started With MongoDB and ASP.Net Step 1: Open the command prompt in administrator mode as shown and go to the MongoDB’s directory. (The folder where we... Step 2 :Type "mongod" and press Enter. And MongoDB has started. It uses port 27017 by default as shown below. Step 3: Now open another ...

How do I add a MongoDB driver to my application?

One way of adding the MongoDB driver to your application is via the CLI. When you install .NET Core/.NET 5.0 on your machine, you get the .NET SDK command line tools out of the box. Run the following command from the root directory of the project, inside your CLI of choice:

How do I install MongoDB on Visual Studio on Mac?

Run the following command to install the .NET driver for MongoDB: In Visual Studio for Mac earlier than version 8.6, select File > New Solution > .NET Core > App from the sidebar. In version 8.6 or later, select File > New Solution > Web and Console > App from the sidebar.

What version of MongoDB do I need for my project?

.NET/C# driver versions 2.8 to 2.13 requires .NET Framework 4.5.2 or later. For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables. If your Visual Studio IDE project depends on the official Mongo C# driver version 2.10 or later, you must use Visual Studio 2017 or later.


2 Answers

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.

enter image description hereenter image description here

like image 163
fatihyildizhan Avatar answered Oct 12 '22 15:10

fatihyildizhan


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; }
        }
    }
}
like image 26
Son_of_Sam Avatar answered Oct 12 '22 16:10

Son_of_Sam