Exception:

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

StackTrace:
   at Xtensive.Orm.Session.Demand()
   at Xtensive.Orm.Query.Single(Key key)
   at Xtensive.Orm.Internals.KeyRemapper.RemapEntityReference(RemapContext context, ReferenceFieldChangeInfo info)
   at Xtensive.Orm.Internals.KeyRemapper.RemapReferencesToEntities(RemapContext context)
   at Xtensive.Orm.Internals.KeyRemapper.Remap(EntityChangeRegistry registry)
   at Xtensive.Orm.Session.Persist(PersistReason reason)
   at Xtensive.Orm.Session.SaveLocalChanges()
   at Xtensive.Orm.Session.SaveChanges()

Entities:

  
[Serializable]
[HierarchyRoot]
public class TestA : Entity
{
  public TestA( Session session ) : base( session ) { }

  [Field, Key]
  public int Id { get; private set; }

  [Field]
  public string Text { get; set; }
}

[Serializable]
[HierarchyRoot]
public class TestB : Entity
{
  public TestB( Session session ) : base( session ) { }

  [Field, Key]
  public int Id { get; private set; }

  [Field]
  public string Text { get; set; }

  [Field]
  public TestA TestA { get; set; }
}

Code:


// Create data  
Xtensive.Orm.Key KeyA2, KeyB;
using( var session = domain.OpenSession( new SessionConfiguration( SessionOptions.ServerProfile ) ) )
{
  using( var transaction = session.OpenTransaction() )
  {
    var TestA1 = new TestA( session ) { Text = "A1" };
    KeyA2 = new TestA( session ) { Text = "A2" }.Key;
    KeyB = new TestB( session ) { Text = "B1", TestA = TestA1 }.Key;
    transaction.Complete();
  }
}

// Exception
using( var session = domain.OpenSession( new SessionConfiguration( SessionOptions.ClientProfile ) ) )
{
  var TestA2 = session.Query.Single<TestA>( KeyA2 );
  var TestB = session.Query.Single<TestB>( KeyB );
  // OK
  TestB.Text = "B2";
  session.SaveChanges();
  // Changing association -> Exception!
  TestB.TestA = TestA2;
  session.SaveChanges();
}

asked Aug 31 '14 at 10:26

TeaMan's gravatar image

TeaMan
140141418

edited Aug 31 '14 at 10:40


One Answer:

Hi TeaMan.

This exception tell that you need to activate session. ClientProfile option does not contain AutoActivation option

For example


using(var session = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile | SessionOptions.AutoActivation))){
//some code
}

or


using (var session = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile)))
using (session.Activate()) {
//some code
}

or


using (var session = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile), true)){
//some code
}

answered Sep 01 '14 at 00:17

Alexey%20Kulakov's gravatar image

Alexey Kulakov
77225

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

powered by OSQA