以下是有关我的开发环境的信息:
在我的C#应用程序中,我有以下代码来检索MongoDB数据库引用:
public class MongoDBConnectionManager {
public IMongoDatabase getMongoDB() {
var client = new MongoClient("mongodb://localhost:27017");
MongoClient(System.Configuration.ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
MongoServer.Create("Server=localhost:27017");
IMongoCollection <BsonDocument> UserDetails = iMgDb.GetCollection<BsonDocument>("Users");
return iMgDb;
}
}
以下是代表用户业务实体的POCO类:
using MongoDB.Bson.Serialization.Attributes;
public class UserModel {
[BsonId]
public int ID { get; set; }
[Required]
[BsonElement]
public string UserName { get; set; }
[Required]
[BsonElement]
public string Password { get; set; }
[Required]
[BsonElement]
public string Email { get; set; }
[BsonElement]
public string PhoneNo { get; set; }
[BsonElement]
public string Address { get; set; }
}
以下是使用Mongo DB连接管理器类的DAO C#类:
public class DAO {
public async Task<int> insertNewUser(UserModel um) {
MongoDBConnectionManager mgoDBCntMng = new MongoDBConnectionManager();
IMongoDatabase database = mgoDBCntMng.getMongoDB();
IMongoCollection <UserModel> UserDetails = database.GetCollection<UserModel>("Users");
try {
Task getTask = UserDetails.InsertOneAsync(um);
await getTask;
} catch(Exception) {
}
return 0;
}
}
当我运行应用程序时,我可以在DOS命令提示符窗口中看到以下信息,其中我启动了mongoDB。如果您查看Dos命令提示符的末尾,您会注意到有2个连接:
C:\Program Files\MongoDB\Server\3.0\bin>mongod --dbpath ./data/db
2015-09-23T12:23:07.896+0530 I JOURNAL [initandlisten] journal
dir=./data/db\jo
urnal
2015-09-23T12:23:07.900+0530 I JOURNAL [initandlisten] recover : no
journal fil
es present, no recovery needed
2015-09-23T12:23:08.060+0530 I JOURNAL [durability] Durability thread started
2015-09-23T12:23:08.062+0530 I JOURNAL [journal writer] Journal writer thread s
tarted
2015-09-23T12:23:08.283+0530 I CONTROL [initandlisten] MongoDB starting
: pid=1
2936 port=27017 dbpath=./data/db 64-bit host=My-PC
2015-09-23T12:23:08.283+0530 I CONTROL [initandlisten] targetMinOS:
Windows 7/W
indows Server 2008 R2
2015-09-23T12:23:08.284+0530 I CONTROL [initandlisten] db version v3.0.6
2015-09-23T12:23:08.284+0530 I CONTROL [initandlisten] git version:
1ef45a23a4c
5e3480ac919b28afcba3c615488f2
2015-09-23T12:23:08.284+0530 I CONTROL [initandlisten] build info:
windows sys.
getwindowsversion(major=6, minor=1, build=7601, platform=2,
service_pack='Servic
e Pack 1') BOOST_LIB_VERSION=1_49
2015-09-23T12:23:08.285+0530 I CONTROL [initandlisten] allocator:
tcmalloc
2015-09-23T12:23:08.285+0530 I CONTROL [initandlisten] options: {
storage: { db
Path: "./data/db" } }
2015-09-23T12:23:08.321+0530 I NETWORK [initandlisten] waiting for
connections
on port 27017
2015-09-23T12:24:20.326+0530 I NETWORK [initandlisten] connection
accepted from
127.0.0.1:65065 #1 (1 connection now open)
2015-09-23T12:24:22.332+0530 I NETWORK [initandlisten] connection
accepted from
127.0.0.1:65066 #2 (2 connections now open)
我真的很难过如何解决问题。我尝试使用DOS命令提示符搜索MongoDB错误日志,但它显示没有错误。 从使用DOS命令提示符的MongoDB客户端,我得到以下内容:
C:\Program Files\MongoDB\Server\3.0\bin>mongo
MongoDB shell version: 3.0.6
connecting to: test
> use foo
switched to db foo
> db.runCommand( { getLastError: 1, w: 1, wtimeout:5000 } )
{
"connectionId" : 6,
"n" : 0,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
我面临的问题是执行点运行顺利,但无法写入数据库 我在上述代码中使用异步和等待的方式出了什么问题?
有人可以告诉我如何纠正这个问题吗?
答案 0 :(得分:2)
来自MSDN
await运算符应用于异步方法中的任务,以暂停方法的执行,直到等待的任务完成。该任务代表了正在进行的工作。
所以等待确实暂停或阻止,直到任务完成,所以我们只创建多个任务,并确保我们不等待它,直到我们认为我们需要,如果你正在处理插入集合?
val a = List(1, List(2,3), 4)
注意:不完全回答,但会清楚我们在做什么
答案 1 :(得分:1)
您的getMongoDB功能代码存在一些问题:
以下是&#34; getMongoDB&#34;的代码功能:
public IMongoDatabase getMongoDB()
{
IMongoClient client = new MongoClient("mongodb://localhost:27017");
var iMgDb = client.GetDatabase("Database Name Here");
return iMgDb;
}
希望这会有所帮助。
答案 2 :(得分:0)
花了一段时间,但问题是由于我错误地在UserModel中使用了int基本数据类型而不是ObjectId。
以下是UserModel的更正代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using WebApplication1.Models;
using MongoDB.Bson.Serialization.Attributes;
namespace WebApplication1.Models
{
public class UserModel
{
[BsonId]
public ObjectId ID { get; set; }
[Required]
[BsonElement]
public string UserName { get; set; }
[Required]
[BsonElement]
public string Password { get; set; }
[Required]
[BsonElement]
public string Email { get; set; }
[BsonElement]
public string PhoneNo { get; set; }
[BsonElement]
public string Address { get; set; }
}
}