DO 5.0.17 In peculiar circumstances (see sample) link field set throw InvalidOperationException

Sample:

using System;
using System.Linq;
using Xtensive.Core;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Services;
using Xtensive.Orm.Upgrade;

internal class Program
{
    private static void Main(string[] args)
    {
        var dc = new DomainConfiguration("sqlserver", "Data Source=.; Initial Catalog=DO40-Tests; Integrated Security=True;");

        dc.Types.Register(typeof(TestEntity));
        dc.Types.Register(typeof(TestEntity1));

        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var d = Domain.Build(dc))
        {
            Init(d);

            // OK
            Test1(d);

            // OK
            Test2(d);

            // OK
            Test3(d);

            // OK
            Test4(d);

            // FAIL
            Test5(d);
        }
    }

    private static void Init(Domain d)
    {
        using (var s = d.OpenSession())
        using (s.Activate())
        using (var t = s.OpenTransaction())
        {
            new TestEntity(s) { String = "test" };
            new TestEntity(s) { String = "test2" };
            new TestEntity(s) { String = "test3" };

            t.Complete();
        }
    }

    private static void Test1(Domain domain)
    {
        using (var s = domain.OpenSession())
        using (s.Activate())
        using (s.OpenTransaction())
        {
            var link = Query.All<TestEntity>().First(e => e.String == "test");
            var link2 = Query.All<TestEntity>().First(e => e.String == "test2");

            var item = new TestEntity1(s) { Link = link };

            Session.Current.Services.Demand<DirectEntityAccessor>()
                .SetFieldValue(item, item.TypeInfo.Fields["Link.Id"], link2.Id);

            item.Link = link;
        }
    }

    private static void Test2(Domain domain)
    {
        using (var s = domain.OpenSession())
        using (s.Activate())
        using (s.OpenTransaction())
        {
            var link = Query.All<TestEntity>().First(e => e.String == "test");
            var link2 = Query.All<TestEntity>().First(e => e.String == "test2");

            var item = new TestEntity1(s) { Link = link };

            item.Link = link2;

            item.Link = link;
        }
    }

    private static void Test3(Domain domain)
    {
        using (var s = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile)))
        using (s.Activate())
        using (s.OpenTransaction())
        {
            var link = Query.All<TestEntity>().First(e => e.String == "test");
            var link2 = Query.All<TestEntity>().First(e => e.String == "test2");

            var item = new TestEntity1(s) { Link = link };

            item.Link = link2;

            item.Link = link;
        }
    }

    private static void Test4(Domain domain)
    {
        using (var s = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile)))
        using (s.Activate())
        using (s.OpenTransaction())
        {
            var link = Query.All<TestEntity>().First(e => e.String == "test");
            var link2 = Query.All<TestEntity>().First(e => e.String == "test2");
            var link3 = Query.All<TestEntity>().First(e => e.String == "test3");

            var item = new TestEntity1(s) { Link = link };

            Session.Current.Services.Demand<DirectEntityAccessor>()
                .SetFieldValue(item, item.TypeInfo.Fields["Link.Id"], link2.Id);

            item.Link = link3;
        }
    }

    private static void Test5(Domain domain)
    {
        using (var s = domain.OpenSession(new SessionConfiguration(SessionOptions.ClientProfile)))
        using (s.Activate())
        using (s.OpenTransaction())
        {
            var link = Query.All<TestEntity>().First(e => e.String == "test");
            var link2 = Query.All<TestEntity>().First(e => e.String == "test2");

            var item = new TestEntity1(s) { Link = link };

            Session.Current.Services.Demand<DirectEntityAccessor>()
                .SetFieldValue(item, item.TypeInfo.Fields["Link.Id"], link2.Id);

            item.Link = link;
        }
    }
}

[HierarchyRoot]
public class TestEntity : Entity
{
    /// <summary>Initializes a new instance of this class.</summary>
    /// <param name="session">The session.</param>
    public TestEntity(Session session) : base(session)
    {
    }

    [Key]
    [Field(Nullable = false)]
    public int Id { get; private set; }

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

[HierarchyRoot]
public class TestEntity1 : Entity
{
    /// <summary>Initializes a new instance of this class.</summary>
    /// <param name="session">The session.</param>
    public TestEntity1(Session session) : base(session)
    {
    }

    [Key]
    [Field(Nullable = false)]
    public int Id { get; private set; }

    [Field]
    public TestEntity Link { get; set; }
}

asked Jul 18 at 05:08

Gushchin%20Anton's gravatar image

Gushchin Anton
75911

Be the first one to answer this question!
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