Simple One-To-One association. Exception occurs after association replace and entity remove. No exception with ServerProfile-Session!

Exception:

  
Referential integrity violation on attempt to remove 'Model.TestA', Key='TestA, (769)'.
Association: TestB-TestA-TestA (AssociationInfo)
Referencing Entity Key: TestB, (771)
Referenced Entity Key: TestA, (769)

Example:

  
[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; }
}

// 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 );
  var TestA1 = TestB.TestA;
  // Changing association
  TestB.TestA = TestA2;
  // Remove entity -> Exception!
  TestA1.Remove();
}

asked Sep 15 '14 at 04:13

TeaMan's gravatar image

TeaMan
140141418

edited Sep 15 '14 at 04:15

Thank you for report, TeaMan.

(Sep 15 '14 at 10:11) Alexey Kulakov Alexey%20Kulakov's gravatar image

2 Answers:

Sorry Alexey, but in the fixed version there are some new errors with Entity.Remove().

Exception:

  
Referential integrity violation on attempt to remove 'Model.TestA', Key='TestA, (2305)'.
Association: TestB-TestA-TestA (AssociationInfo)
Referencing Entity Key: TestB, (2308)
Referenced Entity Key: TestA, (2305)

Example:

  
// Create data  
Xtensive.Orm.Key KeyA2, KeyB1, KeyB2;
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;
    KeyB1 = new TestB( session ) { Text = "B1", TestA = TestA1 }.Key;
    KeyB2 = new TestB( session ) { Text = "B2", TestA = TestA1 }.Key;
    transaction.Complete();
  }
}

// Exception
using( var session = domain.OpenSession( new SessionConfiguration( SessionOptions.ClientProfile ) ) )
{
  var TestA2 = session.Query.Single<TestA>( KeyA2 );
  var TestB1 = session.Query.Single<TestB>( KeyB1 );
  var TestB2 = session.Query.Single<TestB>( KeyB2 );
  TestB2.Remove();
  var TestA1 = TestB1.TestA;
  TestB1.TestA = TestA2;
  // Exception!
  TestA1.Remove();
}

KeyNotFoundException:

  
Entity with Key = 'TestB, (-2)' does not exist.

Example:

  
using( var session = domain.OpenSession( new SessionConfiguration( SessionOptions.ClientProfile ) ) )
{
  var TestA = new TestA( session ) { Text = "A" };
  var TestB = new TestB( session ) { Text = "B", TestA = TestA };
  TestB.Remove();
  // Exception!
  using( session.Activate() )
    session.SaveChanges();
}

Exception with EntitySet:

  
SQL error occured.
SQL error details 'Type: ReferentialConstraintViolation;'
Query 'INSERT INTO [TestA-TestBs-TestB] ([TestA], [TestB]) VALUES (@p0_0, @p0_1); [p0_0='2177';p0_1='-1']'
Original message 'A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_TestA-TestBs-TestB_Slave_TestB ]'

Example:

  
EntitySet-Field within the Entity TestA:

[Field, Association( OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear )]
public EntitySet<testb> TestBs { get; private set; }

using( var session = domain.OpenSession( new SessionConfiguration( SessionOptions.ClientProfile ) ) )
{
  var TestB = new TestB( session ) { Text = "B" };
  var TestA = new TestA( session ) { Text = "A", TestBs = { TestB } };
  TestB.Remove();
  // Exception!
  using( session.Activate() )
    session.SaveChanges();
}

answered Dec 01 '14 at 07:55

TeaMan's gravatar image

TeaMan
140141418

Hi TeaMan.

Thank you for report. I'll check it.

(Dec 02 '14 at 05:48) Alexey Kulakov Alexey%20Kulakov's gravatar image

Hello Alexey, when is the fixed version available?

Thank you.

(Feb 24 '15 at 04:18) TeaMan TeaMan's gravatar image

Any news about?

(Mar 03 '15 at 03:18) TeaMan TeaMan's gravatar image

Hi TeaMan.

We've released 5.0.4 RC. Try it.

(Mar 04 '15 at 03:38) Alexey Kulakov Alexey%20Kulakov's gravatar image

Hello Alexey,

I am a bit late with the feedback, sorry. Now the version 5.0.4 works like a charm!

Thank you.

(Mar 23 '15 at 10:03) TeaMan TeaMan's gravatar image

We've already fixed this bug.

answered Sep 22 '14 at 05:13

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