Пример #1
0
        public static EntitySummary GetEntityEssentials(this IEntityType entityType)
        {
            ITableMappingBase viewMapping  = entityType.GetViewMappings().FirstOrDefault();
            ITableMappingBase tableMapping = entityType.GetTableMappings().FirstOrDefault();
            var isView     = viewMapping != null;
            var schemaName = isView ? entityType.GetViewSchema() : entityType.GetSchema();
            var tableName  = isView ? entityType.GetViewName() : entityType.GetTableName();

            return(new EntitySummary
            {
                IsAbstract = entityType.IsAbstract(),
                IsView = isView,
                Name = entityType.ClrType.Name,
                Schema = schemaName,
                TargetName = tableName
            });
        }
Пример #2
0
        private ITableMappingBase GetTableMapping(IEntityType entityType)
        {
            ITableMappingBase tableMapping = null;

            foreach (var mapping in entityType.GetTableMappings())
            {
                var table = ((ITableMappingBase)mapping).Table;
                if (table.Name == TableName &&
                    table.Schema == Schema)
                {
                    tableMapping = mapping;
                    break;
                }
            }

            return(tableMapping);
        }
Пример #3
0
        private void InitializeSharedColumns(
            IUpdateEntry entry, ITableMappingBase tableMapping, bool updating, Dictionary <string, ColumnValuePropagator> columnMap)
        {
            foreach (var columnMapping in tableMapping.ColumnMappings)
            {
                var columnName = columnMapping.Column.Name;
                if (!columnMap.TryGetValue(columnName, out var columnPropagator))
                {
                    columnPropagator = new ColumnValuePropagator();
                    columnMap.Add(columnName, columnPropagator);
                }

                if (updating)
                {
                    columnPropagator.RecordValue(columnMapping.Property, entry);
                }
            }
        }
Пример #4
0
        private IReadOnlyList <ColumnModification> GenerateColumnModifications()
        {
            var state               = EntityState;
            var adding              = state == EntityState.Added;
            var updating            = state == EntityState.Modified;
            var columnModifications = new List <ColumnModification>();
            Dictionary <string, ColumnValuePropagator> sharedColumnMap = null;

            if (_entries.Count > 1 ||
                (_entries.Count == 1 && _entries[0].SharedIdentityEntry != null))
            {
                sharedColumnMap = new Dictionary <string, ColumnValuePropagator>();

                if (_comparer != null)
                {
                    _entries.Sort(_comparer);
                }

                foreach (var entry in _entries)
                {
                    if (entry.SharedIdentityEntry != null)
                    {
                        InitializeSharedColumns(entry.SharedIdentityEntry, updating, sharedColumnMap);
                    }

                    InitializeSharedColumns(entry, updating, sharedColumnMap);
                }
            }

            foreach (var entry in _entries)
            {
                var nonMainEntry = updating &&
                                   (entry.EntityState == EntityState.Deleted ||
                                    entry.EntityState == EntityState.Added);

                ITableMappingBase tableMapping = null;
                foreach (var mapping in entry.EntityType.GetTableMappings())
                {
                    var table = ((ITableMappingBase)mapping).Table;
                    if (table.Name == TableName &&
                        table.Schema == Schema)
                    {
                        tableMapping = mapping;
                        break;
                    }
                }

                if (tableMapping == null)
                {
                    continue;
                }

                foreach (var columnMapping in tableMapping.ColumnMappings)
                {
                    var property    = columnMapping.Property;
                    var column      = (IColumn)columnMapping.Column;
                    var isKey       = property.IsPrimaryKey();
                    var isCondition = !adding && (isKey || property.IsConcurrencyToken);
                    var readValue   = state != EntityState.Deleted && entry.IsStoreGenerated(property);
                    ColumnValuePropagator columnPropagator = null;
                    if (sharedColumnMap != null)
                    {
                        columnPropagator = sharedColumnMap[column.Name];
                    }

                    var writeValue = false;
                    if (!readValue)
                    {
                        if (adding)
                        {
                            writeValue = property.GetBeforeSaveBehavior() == PropertySaveBehavior.Save;
                        }
                        else if ((updating && property.GetAfterSaveBehavior() == PropertySaveBehavior.Save) ||
                                 (!isKey && nonMainEntry))
                        {
                            writeValue = columnPropagator?.TryPropagate(property, entry)
                                         ?? entry.IsModified(property);
                        }
                    }

                    if (readValue ||
                        writeValue ||
                        isCondition)
                    {
                        if (readValue)
                        {
                            _requiresResultPropagation = true;
                        }

                        var columnModification = new ColumnModification(
                            entry,
                            property,
                            column,
                            _generateParameterName,
                            columnMapping.TypeMapping,
                            readValue,
                            writeValue,
                            isKey,
                            isCondition,
                            _sensitiveLoggingEnabled);

                        if (columnPropagator != null)
                        {
                            if (columnPropagator.ColumnModification != null)
                            {
                                columnPropagator.ColumnModification.AddSharedColumnModification(columnModification);

                                continue;
                            }

                            columnPropagator.ColumnModification = columnModification;
                        }

                        columnModifications.Add(columnModification);
                    }
                }
            }

            return(columnModifications);
        }