Пример #1
0
        public virtual void Detects_non_string_id_property()
        {
            var modelBuilder = CreateConventionlessModelBuilder();

            modelBuilder.Entity <Order>(
                b =>
            {
                b.Property(o => o.Id);
                b.HasKey(o => o.Id);
                b.Property <int>("id");
                b.HasKey("id");
                b.Ignore(o => o.PartitionId);
                b.Ignore(o => o.Customer);
                b.Ignore(o => o.OrderDetails);
                b.Ignore(o => o.Products);
            });

            VerifyError(CosmosStrings.IdNonStringStoreType("id", typeof(Order).Name, "int"), modelBuilder);
        }
Пример #2
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        protected virtual void ValidateKeys(
            [NotNull] IModel model,
            [NotNull] IDiagnosticsLogger <DbLoggerCategory.Model.Validation> logger)
        {
            foreach (var entityType in model.GetEntityTypes())
            {
                var primaryKey = entityType.FindPrimaryKey();
                if (primaryKey == null ||
                    !entityType.IsDocumentRoot())
                {
                    continue;
                }

                var idProperty = entityType.GetProperties()
                                 .FirstOrDefault(p => p.GetJsonPropertyName() == StoreKeyConvention.IdPropertyJsonName);
                if (idProperty == null)
                {
                    throw new InvalidOperationException(CosmosStrings.NoIdProperty(entityType.DisplayName()));
                }

                var idType = idProperty.GetTypeMapping().Converter?.ProviderClrType
                             ?? idProperty.ClrType;
                if (idType != typeof(string))
                {
                    throw new InvalidOperationException(
                              CosmosStrings.IdNonStringStoreType(
                                  idProperty.Name, entityType.DisplayName(), idType.ShortDisplayName()));
                }

                if (!idProperty.IsKey())
                {
                    throw new InvalidOperationException(CosmosStrings.NoIdKey(entityType.DisplayName(), idProperty.Name));
                }

                var partitionKeyPropertyName = entityType.GetPartitionKeyPropertyName();
                if (partitionKeyPropertyName != null)
                {
                    var partitionKey = entityType.FindProperty(partitionKeyPropertyName);
                    if (partitionKey == null)
                    {
                        throw new InvalidOperationException(
                                  CosmosStrings.PartitionKeyMissingProperty(entityType.DisplayName(), partitionKeyPropertyName));
                    }

                    var partitionKeyType = partitionKey.GetTypeMapping().Converter?.ProviderClrType
                                           ?? partitionKey.ClrType;
                    if (partitionKeyType != typeof(string))
                    {
                        throw new InvalidOperationException(
                                  CosmosStrings.PartitionKeyNonStringStoreType(
                                      partitionKeyPropertyName, entityType.DisplayName(), partitionKeyType.ShortDisplayName()));
                    }

                    if (!partitionKey.GetContainingKeys().Any(k => k.Properties.Contains(idProperty)))
                    {
                        throw new InvalidOperationException(
                                  CosmosStrings.NoPartitionKeyKey(
                                      entityType.DisplayName(), partitionKeyPropertyName, idProperty.Name));
                    }
                }
            }
        }