Ejemplo n.º 1
0
        public void StateTests()
        {
            using (var db = new StateDbContext())
            {
                var e = db.Items.Add(new TestItem {
                    Description = "1"
                });
                db.SaveChanges();

                PreEntityEntry afterEntry = null;
                using (db.AfterEach.Subscribe(x => afterEntry = x))
                {
                    var e2 = db.Items.Find(e.Entity.Id);
                    e2.Description = "1";
                    db.SaveChanges();
                    afterEntry.OriginalValues["Description"].Should().Be(afterEntry.Entry.CurrentValues["Description"], "No change should have been detected");

                    var e3 = db.Items.Find(e.Entity.Id);
                    e3.Description = "2";
                    db.SaveChanges();

                    afterEntry.OriginalValues["Description"].Should().Be("1", "Original values in pre-entry did not clone");
                    afterEntry.Entry.OriginalValues["Description"].Should().Be("2", "Original values didn't change in standard entry");
                    afterEntry.Entry.CurrentValues["Description"].Should().Be("2", "Current values didn'tchange in standard entry");
                }
            }
        }
        /// <summary>
        ///     Sets perssistant state
        /// </summary>
        /// <param name="name">Name of state</param>
        /// <param name="value">Value to set or null</param>
        public void SetPersistentState(string name, object?value)
        {
            _ = ConnectionString ??
                throw new NullReferenceException("Connection string cannot be null, please set");

            using (var c = new StateDbContext(ConnectionString))
            {
                var prop = c.States.FirstOrDefault(n => n.PropName == name);

                if (prop is null)
                {
                    if (value is null)
                    {
                        return; // No value allready
                    }
                    var state = new State()
                    {
                        PropName    = name,
                        IntValue    = (value is int) ? (int)value : null,
                        DoubleValue = (value is double) ? (double)value : null,
                        StringValue = (value is string) ? (string)value : null
                    };
                    c.Add(state);
                    c.SaveChanges();
                }
                else
                {
                    // update existing
                    if (value is null)
                    {
                        c.Remove <State>(prop);
                        c.SaveChanges();
                        return;
                    }

                    prop.IntValue    = (value is int) ? (int)value : null;
                    prop.DoubleValue = (value is double) ? (double)value : null;
                    prop.StringValue = (value is string) ? (string)value : null;
                    c.SaveChanges();
                }
            }
        }