RavenDB:如何仅保存特定对象的更改

RavenDB:如何仅保存特定对象的更改,ravendb,Ravendb,在MVC应用程序中,对于请求,我创建一个文档会话,检索一组对象并在内存中处理它们。在此期间,如果出现错误,我将创建一个错误对象并将其存储在Raven中。当我调用SaveChanges来存储这个错误对象时,内存中所有其他对象的状态也会被保存。我需要避免这种情况。如何仅为错误对象激发Savechanges? 我们使用StructureMap获取DocumentSession的实例: public RavenDbRegistry(string connectionStringName) { F

在MVC应用程序中,对于请求,我创建一个文档会话,检索一组对象并在内存中处理它们。在此期间,如果出现错误,我将创建一个错误对象并将其存储在Raven中。当我调用SaveChanges来存储这个错误对象时,内存中所有其他对象的状态也会被保存。我需要避免这种情况。如何仅为错误对象激发Savechanges? 我们使用StructureMap获取DocumentSession的实例:

public RavenDbRegistry(string connectionStringName)
{
    For<IDocumentStore>()
        .Singleton()
        .Use(x =>
        {
            var documentStore = new DocumentStore { ConnectionStringName = connectionStringName };
            documentStore.Initialize();         
            return documentStore;
        }
        )
        .Named("RavenDB Document Store.");
    For<IDocumentSession>()
        .HttpContextScoped()
        .Use(x =>
        {
            var documentStore = x.GetInstance<IDocumentStore>();
            return documentStore.OpenSession();
        })
        .Named("RavenDb Session -> per Http Request.");
}
我尝试过的两种变体没有达到预期效果: 1.仅为错误记录创建新的DocumentSession:

private void SaveError(Error error)
{
    var documentStore = new DocumentStore { ConnectionStringName = "RavenDB" };
    documentStore.Initialize();
    using (var session = documentStore.OpenSession())
    {
        documentSession.Store(error);
        documentSession.SaveChanges();
    }
}
二,。在TransactionScope中包装

private void SaveError(Error error)
{
    using (var tx = new TransactionScope())
    {
        documentSession.Store(error);
        documentSession.SaveChanges();
        tx.Complete();
    }
}
目前我不知道该怎么办。任何帮助都将不胜感激

********更新***********


我可以通过在SaveChanges之前添加以下行来解决此问题

documentSession.Advanced.Clear();.
documentSession.Advanced.Clear()

现在我的SaveError如下所示:

private void SaveError(Models.CMSError error)
        {
            documentSession.Advanced.Clear();
            documentSession.Store(error);
            documentSession.SaveChanges();            
        }
private void SaveError(Models.CMSError error)
        {
            documentSession.Advanced.Clear();
            documentSession.Store(error);
            documentSession.SaveChanges();            
        }

创建新文档会话-但在现有文档存储上,而不是在新文档存储上


注入一个
IDocumentStore
并调用
OpenSession

我能够通过在SaveChanges之前添加以下行来解决问题

documentSession.Advanced.Clear();.
现在我的SaveError如下所示:

private void SaveError(Models.CMSError error)
        {
            documentSession.Advanced.Clear();
            documentSession.Store(error);
            documentSession.SaveChanges();            
        }
private void SaveError(Models.CMSError error)
        {
            documentSession.Advanced.Clear();
            documentSession.Store(error);
            documentSession.SaveChanges();            
        }

谢谢你的答复。我还得试试你的解决办法。就目前而言,我在帖子中提到的改变对我起了作用。