Deleting Document Not Working

590 views
Skip to first unread message

Ryan Zec

unread,
Apr 1, 2011, 8:30:51 AM4/1/11
to ravendb
I have the following code trying to delete a document from the
database inside my repository class:

public bool Remove(IUser entity)
{
using (var session = Store.OpenSession())
{
session.Delete(entity);
session.SaveChanges();
return true;
}
}

The code that called this is:

var loadedUser = UserRepository.Load("bob.jacob2");

var remove = UserRepository.Remove(loadedUser);

The loadedUser is properly retrieiving the data from the data but when
it get to the session.Delete(entity); I get the following error:

UnlockedManagement.WebApplication.Models.Entities.User is not
associated with the session, cannot delete unknown entity instance

Anyone know what would cause this error?

Ayende Rahien

unread,
Apr 1, 2011, 8:39:40 AM4/1/11
to ravendb, Ryan Zec
Put simply, RavenDB has no idea about this entity, so it throws.
You shouldn't use the session in this fashion, to micro manage things. 
The session should be gotten from the ambient context.

Ryan Zec

unread,
Apr 3, 2011, 3:30:26 PM4/3/11
to Ayende Rahien, ravendb
Another stupid mistake on my part with a wrong assumption.  I assumed that OpenSession return an instance of DocumentSession (Not sure why I thought that though) where my constructor should have been defined with IDocumentSession.

On Fri, Apr 1, 2011 at 9:03 AM, Ayende Rahien <aye...@ayende.com> wrote:
        builder.RegisterInstance(session).SingleInstance();

I don't know autofac, but that is probably wrong, you don't have one session, you have one session per request.

On Fri, Apr 1, 2011 at 2:02 PM, Ryan Zec <bas...@gmail.com> wrote:
Ok, maybe someone with autofac experience might be able to help be becuase it does not seem to be working.

I updated my Application_Start to add in the session to autofac:

        var store = new DocumentStore { Url = "http://localhost:8080" };
        store.Initialize();
        var session = store.OpenSession();

        builder.RegisterInstance(store).SingleInstance();
        builder.RegisterInstance(session).SingleInstance();

I then updated my UserRepository constructor to also take in the DocumentSession (left the DocumentStore in there just because but I have tried it without the DocumentStore in there)

        public UserRepository(DocumentStore store, DocumentSession session)


I get the following error

        Cannot resolve parameter 'Raven.Client.Document.DocumentSession Session' of constructor 'Void .ctor(Raven.Client.Document.DocumentStore, Raven.Client.Document.DocumentSession)'

That error seems like autofac can't find the DocumentSession however I include tit the same way I do for the DocumentStore.  Anyone have experience using Autofac with RavenDB?


On Fri, Apr 1, 2011 at 8:44 AM, Ayende Rahien <aye...@ayende.com> wrote:
http://www.dotnetguy.co.uk/post/2010/06/12/raven-db-ndash-part-1-ndash-documentsession-per-request-with-structuremap


On Fri, Apr 1, 2011 at 1:41 PM, Ryan Zec <bas...@gmail.com> wrote:
What do you mean by "ambient context"?  How should I be getting the session?
Reply all
Reply to author
Forward
0 new messages