首页 > 解决方案 > 使用 ArmChair 将新文档添加到 CouchDb 会引发异常 Object reference not set to an instance of an object

问题描述

我正在使用沙发数据库 2.2.0 版

我想couchdb使用 .Net

所以我安装了Armchair.Core Nuget Package 版本 0.11.2

为了添加新文档,我遵循了尚未完成的 wiki 中提到的代码 https://bitbucket.org/dboneslabs/arm-chair/wiki/main-api/session-api.md

Database mydatabase = new Database("TestDb",newConnection("http://localhost:5984"));
            using (var session = mydatabase.CreateSession())
            {
                var author = new Person("Jone");
                session.Add(author);// NOTE: If no Id has been assigned before the instance is added to the Session, then ArmChair will assign it. After the object is committed to the database, the revision will then be set onto the instance

                session.Commit();  
            }

但我仍然收到错误

Object reference not set to an instance of an object.

即使我在构造函数中传递了它们,mydatabase前面代码中提到的变量也有值 nullConnection和参数,因为它根本没有连接到 couchdb 数据库并且从不尝试创建数据库 TestDbDataBase

请提供任何帮助,我的代码中是否有任何错误调用

标签: couchdb-2.0armchair

解决方案


ArmChair 连接到现有数据库并且不创建数据库。

如果要创建数据库,请查看示例应用程序,在 Autofac 注册中有一个方法可以确保创建了数据库。

https://bitbucket.org/dboneslabs/arm-chair/src/bd4e70d6c51d8b45cfb89eb65ecf81a4ecefb691/samples/todo/Todo.Service/Infrastructure/Modules/DataAccessModule.cs#lines-62

它不是最漂亮的代码,但可以工作。


推荐阅读