我使用的是带有.net内核的Mongodb数据库。我只想插入使用mongodbContext的方法。以下是我正在尝试做的事情,但它不起作用: public void InsertEventAsync_Test()
{
//Arrange
var eventRepository = EventRepository();
var pEvent = new PlanEvent
{
ID = "testEvent",
WorkOrderID = "WorkOrderID",
IsDelet
我已经设置了工作单元模式和存储库模式,并且我在其中使用了mongodb。
public class CourseRepository : ICourseRepository
{
private IMongoCollection<Course> _dbContext = null;
public CourseRepository(IMongoCollection<Course> dbContext)
{
_dbContext = dbContext;
}
public async Task Add(Course en
我想通过字符串获取集合,并通过字符串插入许多json。该怎么做呢?我不想通过C#类准备对象,因为我的模式会一直改变,这是一种简单的方法。 代码 const string connectionString = "mongodb://user:password@localhost:27017/myDB";
var client = new MongoClient(connectionString);
var db = client.GetDatabase("myDB");
var col = db.GetCollection("mycollectio
using MongoDB.Bson;
using MongoDB.Driver;
protected static IMongoClient client;
protected static IMongoDatabase db;
public async void Insert()
{
client = new MongoClient();
db = client.GetDatabase("Database");
string json = "[";
for
我设置了从连接字符串到数据库名称的所有错误,但没有抛出异常!
const string ConnectionString = "mongodb://localhoszx";
public IHttpActionResult Post(Lead data)
{
JavaScriptSerializer s = new JavaScriptSerializer();
MongoClient client = new MongoClient(ConnectionString);
IMongoDatabase db
我有C#应用程序,并添加了nugget mongodb驱动程序。
我的mongodb数据库名为DevServers,我的集合名为server
所以我尝试连接,然后插入一个随机文档,因为我假设mongo允许这样做,它与集合中的其他3个文档不匹配/db
var connectionString = "mongodb://root:mypassword@10.100.40.204:27017";
var client = new MongoClient(connectionString);
var db = client.GetDatabase("De
我有这样的代码:
public async void SaveAuditLog(AuditLog a)
{
var db = new MongoDBContext();
var o = db.GetMongoDatabase(Common.Common.MongoDbConnectionString);
var audit = o.GetCollection<AuditLog>("AuditLog");
await audit.InsertOneAsync(a);
}
public IMongoDatabase GetMongo
我正在学习Mongo C#驱动程序的使用。我按照文档和InsertOneAsync代码示例引发了一个异常,如下所示:
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'TestMongo.Program' threw
下面是有关我的开发环境的信息:
Microsoft Visual Studio Community 2015
.NET框架4.6
ASP.NET MVC程序集System.Web.Mvc Version=5.2.3.0
MongoDB.Driver 2.0.1.27
Mongodb 3.0.6
在我的C#应用程序中,我有以下检索MongoDB数据库引用的代码:
public class MongoDBConnectionManager
{
public IMongoDatabase getMongoDB() {
var client = new Mon
我正在尝试使用Moq创建一个测试提供程序的单元测试。这句话让我很难过:
var appUser = new Mock<PreRegistrationMVC.Models.ApplicationUser>();
var userStore = new Mock<MongoDB.AspNet.Identity.UserStore<PreRegistrationMVC.Models.ApplicationUser>>();
看起来userStore不能正确地实例化,这是错误。
Castle.DynamicProxy.InvalidProxyConstructorA
我有连接到MongoDB的VB.net代码。当数据库启动并运行时,我的代码工作得很好,但当数据库不运行时,我不会收到任何错误。
如何检查服务器是否已启动并正在运行,以便可以连接到它并进行工作?基本上,如果服务器已启动,请做其他工作,否则将向用户返回一条消息,表明服务器不可用。
我查看了有关MongoClient类的文档,但似乎找不到任何我可以使用的东西。
MongoClient类()
下面是我用来连接到MongoDB的代码:
Public Function DbConnection(ByRef ConnString As String, vDbName As String, vColName
我有一个通过获取一个MongoDb并将原始字节写入Azure流来归档IAsyncCursor集合的过程。这似乎是相当有效和有效的。这是工作代码。
var cursor = await clientDb.GetCollection<RawBsonDocument>(collectionPath).Find(new BsonDocument()).ToCursorAsync();
while (cursor.MoveNext())
foreach (var document in cursor.Current)
{
var bytes = new byt
我将MongoDB 4.0.8与C#驱动程序2.8.1一起使用,并试图在我的项目中实现Transactions。我复制-粘贴了以下代码示例:
static async Task<bool> UpdateProducts()
{
//Create client connection to our MongoDB database
var client = new MongoClient(MongoDBConnectionString);
//Create a session object that is used when leveraging transa
我不是nosql专家,但一年前我使用以下代码创建了一个mongodb表:
const string connectionString = "mongodb://localhost:27017";
// Create a MongoClient object by using the connection string
var client = new MongoClient(connectionString);
////Use the MongoClient to access the
我在我的MongoDB.Drivers MVC应用程序中使用C#与Mongodb数据库进行通信。
C#码
var client = new MongoClient("mongodb://localhost:27012");
var db = client.GetDatabase("Test_DB");
var collection = db.GetCollection<BsonDocument>("TestTable");
var tDat
我使用C#驱动程序V2.9.3将一个项目保存在MongoDB中。 我看到下面的异常偶尔会被抛出(虽然一旦它发生一次,它似乎就会更轻微地再次发生)。 System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at MongoDB.Bson.Serialization.Serializers.DictionarySerial
表示层调用我的应用层中的方法(CreateEvent)。此方法使用泛型参数:
public async Task<string> CreateEvent<T, TDocument>(T @event)
where T : class
where TDocument : Document
{
using (var scope = _serviceProvider.CreateScope())
{
var myRepository = scope.ServiceProvider.GetRe
根据
在无效事件(例如,集合删除或重命名)关闭流后,不能使用resumeAfter恢复更改流。从MongoDB 4.2开始,您可以使用startAfter在无效事件之后启动新的更改流。
我使用此代码恢复集合重命名后的更改流:
var collection = db.GetCollection<Student>("bla");
var student1 = new Student();
var are = new AutoResetEvent(false);
//RESUME TOKEN
BsonDocument resumeToken = null;
v
我们在我们的项目中有一个webAPI,它非常简单,但它在执行后不释放内存,每次执行时,它都会增加已用内存。
我使用webAPI 2.0和MongoDB作为后端服务器。
public class LogsController:ApiController
{
BsonDocument __docs;
IMongoDatabase __db;
IMongoCollection<BsonDocument> __docsColl;
[Route("api/Logs")]
public async Task<int> Post
我使用以下逻辑(MOQ)来尝试模拟MongoDB csharp驱动程序对象:
var svr = new Mock<MongoServer>(new MongoServerSettings());
var db = new Mock<MongoDatabase>(svr.Object, new MongoDatabaseSettings("hf_test",
new MongoCredentials("hf_test", "hf_pass"), GuidRepresentation.Standard,
我在我的linux机器上尝试使用.net核心的mongo db和使用visual studio代码的.net驱动程序。我能够创建dbs,使用dbs,查询它们,我可以用我的终端做任何事情。但是,当我从控制台查看时,我在代码中使用.net驱动程序创建的DB是不可见的。这可能是什么原因呢?
这是我正在使用的代码。
using System;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDBDemo
{
class Program
{
static void Main(string[] args)