In ClientProfile session entity link field cannot be set equal null, if earlier field value has been set by DirectEntityAccessor

Field should have paired entity set

Example

using System.Linq;
using NUnit.Framework;
using Xtensive.Core;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Services;

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(TestEntity2));

        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var d = Domain.Build(dc))
        {
            using (var s = d.OpenSession())
            using (s.Activate())
            using (var t = s.OpenTransaction())
            {
                new TestEntity2(s);
                new TestEntity3(s);

                t.Complete();
            }

            using (var s = d.OpenSession())
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var item = new TestEntity(s);

                item.Link2 = s.Query.All<TestEntity2>().First();
                item.Link3 = s.Query.All<TestEntity3>().First();

                Assert.NotNull(item.Link2);
                Assert.NotNull(item.Link3);

                item.Link2 = null;
                item.Link3 = null;

                Assert.Null(item.Link2);
                Assert.Null(item.Link3);
            }

            using (var s = d.OpenSession())
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var item = new TestEntity(s);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link2.Id"], s.Query.All<TestEntity2>().First().Id);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link3.Id"], s.Query.All<TestEntity3>().First().Id);

                Assert.NotNull(item.Link2);
                Assert.NotNull(item.Link3);

                item.Link2 = null;
                item.Link3 = null;

                Assert.Null(item.Link2);
                Assert.Null(item.Link3);
            }

            var config = d.Configuration.Sessions.Default.Clone();

            config.Options = SessionOptions.ClientProfile;
            using (var s = d.OpenSession(config))
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var item = new TestEntity(s);

                item.Link2 = s.Query.All<TestEntity2>().First();
                item.Link3 = s.Query.All<TestEntity3>().First();

                Assert.NotNull(item.Link2);
                Assert.NotNull(item.Link3);

                item.Link2 = null;
                item.Link3 = null;

                Assert.Null(item.Link2);
                Assert.Null(item.Link3);
            }

            using (var s = d.OpenSession(config))
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var item = new TestEntity(s);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link2.Id"], s.Query.All<TestEntity2>().First().Id);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link3.Id"], s.Query.All<TestEntity3>().First().Id);

                Assert.NotNull(item.Link2);
                Assert.NotNull(item.Link3);

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

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

                Assert.Null(item.Link2);
                Assert.Null(item.Link3);
            }

            using (var s = d.OpenSession(config))
            using (s.Activate())
            using (s.OpenTransaction())
            {
                var item = new TestEntity(s);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link2.Id"], s.Query.All<TestEntity2>().First().Id);

                Session.Current.Services.Demand<DirectEntityAccessor>()
                    .SetFieldValue(item, item.TypeInfo.Fields["Link3.Id"], s.Query.All<TestEntity3>().First().Id);

                Assert.NotNull(item.Link2);
                Assert.NotNull(item.Link3);

                item.Link2 = null;
                item.Link3 = null;

                Assert.Null(item.Link2);
                Assert.Null(item.Link3);
            }
        }
    }

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

        /// <summary>
        /// Link2
        /// </summary>
        [Field]
        public TestEntity2 Link2 { get; set; }

        /// <summary>
        /// Link3
        /// </summary>
        [Field]
        public TestEntity3 Link3 { get; set; }
    }

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

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

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

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

        /// <summary>
        /// Link
        /// </summary>
        [Field]
        [Association(PairTo = "Link3")]
        public EntitySet<TestEntity> EntitySet { get; set; }
    }
}

asked Oct 29 '19 at 02:47

Gushchin%20Anton's gravatar image

Gushchin Anton
11272729

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