The manual says that you can create a record as such:

using (Session.Open(domain))
{
    using (var transactionScope = Transaction.Open())
    {
  // Creating new persistent object
  var helloWorld = new MyEntity
  {
      Text = "Hello World!"
  };

        // Committing transaction
  transactionScope .Complete();
    }
}

However when I do this I get the following exception:

Active Session is required for this operation. Use Session.Open(...) to open it.

The sample code implements a constructor that takes session which does work:

public MyEntity(Session session) : base(session) {}

Has this changed in the 4.4 beta since the manual was written? What am I doing wrong? Do I have to implement this constructor in all my classes?

asked Dec 22 '10 at 16:47

slamb2k's gravatar image

slamb2k
5222

edited Jan 04 '11 at 16:02

Sergey's gravatar image

Sergey
123339


One Answer:

As far as I have grasped this, you either have to implement such a constructor for entities that passes the session object to the base constructor or you have to utilize automatic session activation - like this:

DomainConfiguration domainConfiguration = DomainConfiguration.Load(...);
domainConfiguration.Types.Register(typeof(...).Assembly, typeof(...).Namespace);
SessionConfiguration sessionConfiguration = new SessionConfiguration(SessionOptions.AutoActivation);
domainConfiguration.Sessions.Add(sessionConfiguration);
Domain domain = Domain.Build(domainConfiguration);
using (var session = domain.OpenSession(sessionConfiguration))
using (var transactionScope = session.OpenTransaction())
{
    // create Entities
    transactionScope.Complete();
}

answered Dec 22 '10 at 17:43

Sebastian%20Hauer's gravatar image

Sebastian Hauer
87113

Thanks, Sebastian, you are absolutely right.

The only thing I could add is that it might be more convenient to make use of Session Profiles (Client, Server & Legacy ones). A Session Profile notion represents a set of predefined Session Options and is introduced in 4.4 Beta 1. More information on the subject can be found in Release Notes.

(Dec 22 '10 at 23:19) Dmitri Maximov Dmitri%20Maximov's gravatar image

I am using 4.4 Beta 1 and I hadn't read the release notes. Thanks for answering the question so quickly. Just a few more questions:

  • Is the new session profile without automatic session creation included to ensure thread safety?

  • Would be have problems if we use the legacy mode with multiple threads?

  • If we use the new session profile do we have to create constructors that take a session as a parameter in all of our entities?

(Dec 23 '10 at 16:53) slamb2k slamb2k's gravatar image
1
  1. Session is not thread-safe in any way
  2. You won't have any problems. The only thing you should care about is session activation and deactivation within a thread
  3. There are 2 options: you create constructors that take a session instance as a parameter or you use the following pattern with temporary Session activation and construct your entities inside this region, e.g.:

    using (session.Activate()) {

    // Here we can use parameterless constrictor

    var person = new Person();

    }

Hope that helps.

(Dec 24 '10 at 05:47) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×573
×9
×2

Asked: Dec 22 '10 at 16:47

Seen: 3,528 times

Last updated: Jan 04 '11 at 16:02

powered by OSQA