Пример #1
0
        public virtual EntityEntry FindEntry(object entity)
        {
            IEntityServices entityServices = _entityServicesFactory.GetEntityServices(entity.GetType());
            KeyValue        keyValue       = entityServices.GetKeyValue(entity);

            IIdentityMap map = _findIdentityMap(entityServices.GetKey());

            if (map != null)
            {
                var existing = map.TryGetEntry(keyValue.Values);
                if (existing != null)
                {
                    return(new EntityEntry(existing));
                }
            }
            return(null);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected virtual void ValidateData([NotNull] IModel model)
        {
            Check.NotNull(model, nameof(model));

            var identityMaps        = new Dictionary <IKey, IIdentityMap>();
            var sensitiveDataLogged = Dependencies.Logger.ShouldLogSensitiveData();

            foreach (var entityType in model.GetEntityTypes().Where(et => !et.IsQueryType))
            {
                var          key         = entityType.FindPrimaryKey();
                IIdentityMap identityMap = null;
                foreach (var seedDatum in entityType.GetData())
                {
                    foreach (var property in entityType.GetProperties())
                    {
                        if (!seedDatum.TryGetValue(property.Name, out var value) ||
                            value == null)
                        {
                            if (!property.IsNullable &&
                                (!property.RequiresValueGenerator() ||
                                 property.IsKey()))
                            {
                                throw new InvalidOperationException(CoreStrings.SeedDatumMissingValue(entityType.DisplayName(), property.Name));
                            }
                        }
                        else if (property.RequiresValueGenerator() &&
                                 property.IsKey() &&
                                 property.ClrType.IsDefaultValue(value))
                        {
                            if (property.ClrType.IsSignedInteger())
                            {
                                throw new InvalidOperationException(CoreStrings.SeedDatumSignedNumericValue(entityType.DisplayName(), property.Name));
                            }
                            throw new InvalidOperationException(CoreStrings.SeedDatumDefaultValue(entityType.DisplayName(), property.Name, property.ClrType.GetDefaultValue()));
                        }
                        else if (!property.ClrType.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo()))
                        {
                            if (sensitiveDataLogged)
                            {
                                throw new InvalidOperationException(
                                          CoreStrings.SeedDatumIncompatibleValueSensitive(
                                              entityType.DisplayName(), value, property.Name, property.ClrType.DisplayName()));
                            }

                            throw new InvalidOperationException(
                                      CoreStrings.SeedDatumIncompatibleValue(
                                          entityType.DisplayName(), property.Name, property.ClrType.DisplayName()));
                        }
                    }

                    var keyValues = new object[key.Properties.Count];
                    for (var i = 0; i < key.Properties.Count; i++)
                    {
                        keyValues[i] = seedDatum[key.Properties[i].Name];
                    }

                    foreach (var navigation in entityType.GetNavigations())
                    {
                        if (seedDatum.TryGetValue(navigation.Name, out var value) &&
                            ((navigation.IsCollection() && value is IEnumerable collection && collection.Any()) ||
                             (!navigation.IsCollection() && value != null)))
                        {
                            if (sensitiveDataLogged)
                            {
                                throw new InvalidOperationException(
                                          CoreStrings.SeedDatumNavigationSensitive(
                                              entityType.DisplayName(),
                                              string.Join(", ", key.Properties.Select((p, i) => p.Name + ":" + keyValues[i])),
                                              navigation.Name,
                                              navigation.GetTargetType().DisplayName(),
                                              Property.Format(navigation.ForeignKey.Properties)));
                            }

                            throw new InvalidOperationException(
                                      CoreStrings.SeedDatumNavigation(
                                          entityType.DisplayName(),
                                          navigation.Name,
                                          navigation.GetTargetType().DisplayName(),
                                          Property.Format(navigation.ForeignKey.Properties)));
                        }
                    }

                    if (identityMap == null)
                    {
                        if (!identityMaps.TryGetValue(key, out identityMap))
                        {
                            identityMap       = key.GetIdentityMapFactory()(sensitiveDataLogged);
                            identityMaps[key] = identityMap;
                        }
                    }

                    var entry = identityMap.TryGetEntry(keyValues);
                    if (entry != null)
                    {
                        if (sensitiveDataLogged)
                        {
                            throw new InvalidOperationException(
                                      CoreStrings.SeedDatumDuplicateSensitive(
                                          entityType.DisplayName(), string.Join(", ", key.Properties.Select((p, i) => p.Name + ":" + keyValues[i]))));
                        }

                        throw new InvalidOperationException(
                                  CoreStrings.SeedDatumDuplicate(
                                      entityType.DisplayName(), Property.Format(key.Properties)));
                    }

                    entry = new InternalShadowEntityEntry(null, entityType);

                    identityMap.Add(keyValues, entry);
                }
            }
        }