Esempio n. 1
0
 public virtual void EnsureRelationshipSnapshot()
 {
     if (_relationshipsSnapshot.IsEmpty)
     {
         _relationshipsSnapshot = new RelationshipsSnapshot(this);
     }
 }
        public void Creates_a_new_key_from_a_sidecar_value()
        {
            var model = BuildModel();
            var type = model.FindEntityType(typeof(Banana));
            var stateManager = TestHelpers.Instance.CreateContextServices(model).GetRequiredService<IStateManager>();

            var entity = new Banana { P1 = 7, P2 = 77 };
            var entry = stateManager.GetOrCreateEntry(entity);

            var sidecar = new RelationshipsSnapshot(entry);
            sidecar[type.FindProperty("P2")] = 78;

            var key = (SimpleKeyValue<int>)new SimpleKeyValueFactory<int>(type.FindPrimaryKey())
                .Create(new[] { type.FindProperty("P2") }, sidecar);

            Assert.Equal(78, key.Value);
        }
        public void Creates_a_new_key_for_values_from_a_sidecar()
        {
            var model = BuildModel();
            var type = model.GetEntityType(typeof(Banana));
            var stateManager = TestHelpers.Instance.CreateContextServices(model).GetRequiredService<IStateManager>();

            var random = new Random();
            var entity = new Banana { P4 = 7, P5 = "Ate", P6 = random };

            var entry = stateManager.GetOrCreateEntry(entity);

            var sidecar = new RelationshipsSnapshot(entry);
            sidecar[type.GetProperty("P4")] = 77;

            var key = (CompositeEntityKey)new CompositeEntityKeyFactory(
                new object[] { null, 0, null })
                .Create(type, new[] { type.GetProperty("P6"), type.GetProperty("P4"), type.GetProperty("P5") }, sidecar);

            Assert.Equal(new object[] { random, 77, "Ate" }, key.Value);
        }
        public void Creates_a_new_key_from_current_value_when_value_not_yet_set_in_sidecar()
        {
            var model = BuildModel();
            var type = model.GetEntityType(typeof(Banana));
            var stateManager = TestHelpers.Instance.CreateContextServices(model).GetRequiredService<IStateManager>();

            var entity = new Banana { P1 = 7, P2 = 77 };
            var entry = stateManager.GetOrCreateEntry(entity);

            var sidecar = new RelationshipsSnapshot(entry);

            var key = (SimpleEntityKey<int>)new SimpleEntityKeyFactory<int>(type.GetPrimaryKey(), 0)
                .Create(new[] { type.GetProperty("P2") }, sidecar);

            Assert.Equal(77, key.Value);
        }
        public void Creates_a_new_key_from_a_sidecar_value()
        {
            var model = BuildModel();
            var type = model.GetEntityType(typeof(Banana));
            var stateManager = TestHelpers.Instance.CreateContextServices(model).GetRequiredService<IStateManager>();

            var entity = new Banana { P1 = 7, P2 = 8 };
            var entry = stateManager.GetOrCreateEntry(entity);

            var sidecar = new RelationshipsSnapshot(entry);
            sidecar[type.GetProperty("P2")] = "Eaten";

            var key = (SimpleEntityKey<string>)new SimpleEntityKeyFactory<string>(null).Create(
                type, new[] { type.GetProperty("P2") }, sidecar);

            Assert.Equal("Eaten", key.Value);
        }