Пример #1
0
        private static ChangeTrackingEntry <T> mapToChangeTrackingEntry <T>(DbDataReader reader, IEntityType entityType) where T : class, new()
        {
            var entity = new T();
            var entry  = new ChangeTrackingEntry <T>(entity);

            entry.ChangeContext   = reader[nameof(ChangeTrackingEntry <T> .ChangeContext)] as string;
            entry.ChangeVersion   = (long)reader[nameof(ChangeTrackingEntry <T> .ChangeVersion)];
            entry.CreationVersion = reader[nameof(ChangeTrackingEntry <T> .CreationVersion)] as long?;

            var operation = (string)reader[nameof(ChangeTrackingEntry <T> .ChangeOperation)];

            entry.ChangeOperation = operation switch
            {
                "I" => ChangeOperation.Insert,
                "U" => ChangeOperation.Update,
                "D" => ChangeOperation.Delete,
                _ => throw new Exception()
            };

            foreach (var propertyInfo in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var columnName = entityType.GetProperties().First(p => p.Name == propertyInfo.Name).GetColumnName();

                object?readerValue = reader[columnName];

                readerValue = readerValue == DBNull.Value ? null : readerValue;

                propertyInfo.SetValue(entity, readerValue);
            }

            return(entry);
        }
Пример #2
0
        /// <inheritdoc />
        public IChangeTrackingEntry EnsureEntry(object target, bool autoAttach)
        {
            var key = CalculateKey(target);

            if (_entryDict.TryGetValue(key, out var entry))
            {
                return(entry);
            }

            var snapshot = _snapshotFactory.CreateSnapshot(target);

            entry = new ChangeTrackingEntry(key, this, snapshot, autoAttach ? TrackedState.Unchanged : TrackedState.Detached);
            if (autoAttach)
            {
                _entryDict.Add(key, entry);
            }

            return(entry);
        }