我一直在使用asp.net mvc的創建MongoDB中一個數據庫,但是當我看到我的數據庫插入失敗。我已經在一個控制檯應用程序之前測試創建MongoDB中的數據庫,它工作正常,但現在!當我使用MVC我不覺得這個問題你能幫助我請 這是我的代碼使用asp.net的MVC
//IDeviseRepository
namespace WebApplication6
{
public interface IDeviseRepository
{
Devise Add(Devise devise);
void MongoGoNow();
}
}
//DeviseRepository
namespace WebApplication6
{
public class DeviseRepository : IDeviseRepository
{
public IMongoDatabase db;
public DeviseRepository()
{
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress("localhost", 27017);
MongoClient client = new MongoClient(settings);
this.db = client.GetDatabase("bigdata");
var collection = db.GetCollection<Devise>("Devise");
}
public IMongoCollection<Devise> Devise
{
get
{ return db.GetCollection<Devise>("Devise"); }
}
public Devise Add(Devise devise)
{
var collection = db.GetCollection<Devise>("Devise");
collection.InsertOne(devise);
return devise;
}
public void MongoGoNow()
{
var collection = db.GetCollection<Devise>("devise");
var result = TestFind(collection);
result.GetAwaiter().GetResult();
}
static async Task TestFind(IMongoCollection<Devise> MyCollection)
{
var filter = new BsonDocument();
var count = 0;
using (var cursor = await MyCollection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
count++;
}
}
}
}
public void insertdata()
{
var devise = new Devise();
devise.parité = "euro/dollar";
Devise.InsertOne(devise);
}
}
}
它就像你正在使用的舊蒙戈驅動程序在我看來。我首先建議,在升級到2.0:https://docs.mongodb.org/ecosystem/drivers/csharp/ – pimbrouwers
另外,如果你換你插入一個try/catch塊會發生什麼? – pimbrouwers
的[如何創建MongoDB中使用asp.net mvc的數據庫]可能的複製(http://stackoverflow.com/questions/36251180/how-to-create-a-database-in-mongodb-with-asp-net -mvc) – Saleem