public async Task SetReferenceToNullFromOppositeTest()
        {
            var store = await StoreBuilder.New().CreateAsync();

            var schema = await store.Schemas.New <TestDomainDefinition>().CreateAsync();

            var dm = await store.DomainModels.New().CreateAsync("Test");

            XExtendsBaseClass x = null;
            YClass            y = null;

            using (var s = store.BeginSession())
            {
                x = new XExtendsBaseClass(dm);
                y = new YClass(dm)
                {
                    Name = "1"
                };
                x.YRelation = y;
                s.AcceptChanges();
            }

            var rel = x.GetRelationships <XReferencesY>().FirstOrDefault();

            using (var s = store.BeginSession())
            {
                y.X = null;
                s.AcceptChanges();
            }

            Assert.Equal(x.YRelation, null);
            Assert.Equal(((IModelElement)y).Status, ModelElementStatus.Disposed);
            Assert.Equal(x.GetRelationships <XReferencesY>().Count(), 0);
        }
        public async Task SetReferenceToNullFromOpposite2Test()
        {
            var store = await StoreBuilder.New().CreateAsync();

            var schema = await store.Schemas.New <TestDomainDefinition>().CreateAsync();

            var dm = await store.DomainModels.New().CreateAsync("Test");

            XExtendsBaseClass x = null;
            YClass            y = null;

            using (var s = store.BeginSession())
            {
                x = new XExtendsBaseClass(dm);
                y = new YClass(dm)
                {
                    Name = "1"
                };
                new XReferencesY(x, y);
                s.AcceptChanges();
            }

            var rel = x.GetRelationships <XReferencesY>().FirstOrDefault();

            using (var s = store.BeginSession())
            {
                y.X = null; // TODO Since it's an embedded relationship and y is the opposite, y is deleted. Is it the wright behavior ???
                s.AcceptChanges();
            }

            Assert.Equal(x.YRelation, null);
            Assert.Equal(x.GetRelationships <XReferencesY>().Count(), 0);

            using (var s = store.BeginSession())
            {
                y = new YClass(dm)
                {
                    Name = "1"
                };
                new XReferencesY(x, y);
                s.AcceptChanges();
            }

            rel = x.GetRelationships <XReferencesY>().FirstOrDefault();
            using (var s = store.BeginSession())
            {
                x.YRelation = null;
                s.AcceptChanges();
            }

            Assert.Equal(x.YRelation, null);
            Assert.Equal(((IModelElement)y).Status, ModelElementStatus.Disposed);
            Assert.Equal(x.GetRelationships <XReferencesY>().Count(), 0);
        }
        public async Task SetReferenceTest()
        {
            var store = await StoreBuilder.New().CreateAsync();

            var schema = await store.Schemas.New <TestDomainDefinition>().CreateAsync();

            var dm = await store.DomainModels.New().CreateAsync("Test");

            XExtendsBaseClass x = null;
            YClass            y = null;

            using (var s = store.BeginSession())
            {
                x = new XExtendsBaseClass(dm);
                y = new YClass(dm)
                {
                    Name = "1"
                };
                x.YRelation = y;
                s.AcceptChanges();
            }

            Assert.Equal(x.YRelation, y);
            Assert.Equal(x.YRelation.X, x);
            Assert.Equal(y.X, x);
            var rel = x.GetRelationships <XReferencesY>().FirstOrDefault();

            Assert.Equal(rel.Start, x);
            Assert.Equal(rel.End, y);

            using (var s = store.BeginSession())
            {
                y = new YClass(dm)
                {
                    Name = "2"
                };
                x.YRelation = y;
                s.AcceptChanges();
            }

            Assert.Equal(x.YRelation, y);
            Assert.Equal(x.YRelation.X, x);
            Assert.Equal(x.GetRelationships <XReferencesY>().Count(), 1);

            rel = x.GetRelationships <XReferencesY>().FirstOrDefault();
            Assert.Equal(rel.Start, x);
            Assert.Equal(rel.End, y);
        }
        public async Task PropertyChangedOnSetReferenceTest()
        {
            var store = await StoreBuilder.New().CreateAsync();

            var schema = await store.Schemas.New <TestDomainDefinition>().CreateAsync();

            var dm = await store.DomainModels.New().UsingIdGenerator(r => new LongIdGenerator()).CreateAsync("Test");

            XExtendsBaseClass x = null;
            YClass            y = null;

            var yrelationChanges   = 0;
            var allPropertychanges = 0;

            using (var s = store.BeginSession())
            {
                x = new XExtendsBaseClass(dm);
                s.AcceptChanges();
            }

            x.PropertyChanged += (sender, e) =>
            {
                allPropertychanges++;
                if (e.PropertyName == "YRelation")
                {
                    yrelationChanges++;
                }
            };

            using (var s = store.BeginSession())
            {
                y = new YClass(dm)
                {
                    Name = "1"
                };
                x.YRelation = y;
                s.AcceptChanges();
            }

            using (var s = store.BeginSession())
            {
                y = new YClass(dm)
                {
                    Name = "2"
                };
                x.YRelation = y;
                s.AcceptChanges();
            }

            var rel = x.GetRelationships <XReferencesY>().FirstOrDefault();

            using (var s = store.BeginSession())
            {
                x.YRelation = null;
                s.AcceptChanges();
            }

            Assert.Equal(3, yrelationChanges);
            Assert.Equal(3, allPropertychanges);
        }