/// <inheritdoc />
        public override InternalEntityTypeBuilder Apply(
            InternalEntityTypeBuilder entityTypeBuilder,
            BsonKnownTypesAttribute bsonKnownTypesAttribute)
        {
            MongoDbEntityTypeAnnotations annotations = entityTypeBuilder.MongoDb();

            if (!annotations.DiscriminatorIsRequired)
            {
                annotations.DiscriminatorIsRequired = entityTypeBuilder.Metadata.IsAbstract();
            }

            if (bsonKnownTypesAttribute.KnownTypes != null)
            {
                InternalModelBuilder modelBuilder = entityTypeBuilder.ModelBuilder;
                Type baseType = entityTypeBuilder.Metadata.ClrType;

                foreach (Type derivedType in bsonKnownTypesAttribute.KnownTypes)
                {
                    if (!baseType.IsAssignableFrom(derivedType))
                    {
                        throw new InvalidOperationException($"Known type {derivedType} declared on base type {baseType} does not inherit from base type.");
                    }

                    modelBuilder
                    .Entity(derivedType, ConfigurationSource.DataAnnotation)
                    .MongoDb()
                    .IsDerivedType = true;
                }
            }

            return(entityTypeBuilder);
        }
        /// <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>
        public InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder)
        {
            EntityType         baseEntityType = Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)).Metadata;
            IEnumerable <Type> knownTypes     = baseEntityType.ClrType
                                                ?.GetTypeInfo()
                                                .GetCustomAttributes <BsonKnownTypesAttribute>(false)
                                                .SelectMany(bsonKnownTypeAttribute => bsonKnownTypeAttribute.KnownTypes)
                                                .ToList();

            MongoDbEntityTypeAnnotations annotations = entityTypeBuilder.MongoDb();

            if (!annotations.DiscriminatorIsRequired)
            {
                annotations.DiscriminatorIsRequired = baseEntityType.IsAbstract();
            }

            if (knownTypes != null)
            {
                InternalModelBuilder modelBuilder = entityTypeBuilder.ModelBuilder;
                foreach (Type derivedType in knownTypes)
                {
                    modelBuilder
                    .Entity(derivedType, ConfigurationSource.DataAnnotation)
                    .HasBaseType(baseEntityType, ConfigurationSource.DataAnnotation)
                    .MongoDb()
                    .IsDerivedType = true;
                }
            }
            return(entityTypeBuilder);
        }
Ejemplo n.º 3
0
        public void Discriminator_is_type_name_by_default()
        {
            var model      = new Model();
            var entityType = new EntityType(typeof(RootType), model, ConfigurationSource.Explicit);
            var mongoDbEntityTypeAnnotations = new MongoDbEntityTypeAnnotations(entityType);

            Assert.Equal(typeof(RootType).Name, mongoDbEntityTypeAnnotations.Discriminator);
        }
Ejemplo n.º 4
0
        public void Collection_name_is_pluralized_camel_cased_entity_type_by_default()
        {
            var model      = new Model();
            var entityType = new EntityType(typeof(RootType), model, ConfigurationSource.Explicit);
            var mongoDbEntityTypeAnnotations = new MongoDbEntityTypeAnnotations(entityType);

            Assert.Equal(MongoDbUtilities.Pluralize(MongoDbUtilities.ToCamelCase(nameof(RootType))),
                         mongoDbEntityTypeAnnotations.CollectionName);
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public virtual IMongoCollection <TEntity> GetCollection <TEntity>()
        {
            IEntityType collectionEntityType = _model
                                               .FindEntityType(typeof(TEntity))
                                               .GetMongoDbCollectionEntityType();

            MongoDbEntityTypeAnnotations annotations = collectionEntityType.MongoDb();

            return(_mongoDatabase.GetCollection <TEntity>(annotations.CollectionName, annotations.CollectionSettings));
        }
Ejemplo n.º 6
0
        private IEntityType GetCollectionEntityType(IEntityType entityType)
        {
            MongoDbEntityTypeAnnotations annotations = entityType.MongoDb();

            while (annotations.IsDerivedType && entityType.BaseType != null)
            {
                entityType  = entityType.BaseType;
                annotations = entityType.MongoDb();
            }
            return(entityType);
        }
Ejemplo n.º 7
0
        public virtual IMongoCollection <TEntity> GetCollection <TEntity>()
        {
            var entityType = _model.FindEntityType(typeof(TEntity));

            if (entityType.BaseType != null)
            {
                entityType = entityType.RootType();
            }
            var annotations = new MongoDbEntityTypeAnnotations(entityType);

            return(_mongoDatabase.GetCollection <TEntity>(annotations.CollectionName, annotations.CollectionSettings));
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Gets a <see cref="IMongoCollection{TEntity}"/> instance that can be used to store instances of <typeparamref name="TEntity"/>.
        /// </summary>
        /// <typeparam name="TEntity">The type of entity stored in the collection.</typeparam>
        /// <returns>The <see cref="IMongoCollection{TEntity}"/> instance that can store <typeparamref name="TEntity"/>.</returns>
        public virtual IMongoCollection <TEntity> GetCollection <TEntity>()
        {
            IEntityType entityType = _model.FindEntityType(typeof(TEntity));
            MongoDbEntityTypeAnnotations annotations = entityType.MongoDb();

            while (annotations.IsDerivedType && entityType.BaseType != null)
            {
                entityType  = entityType.BaseType;
                annotations = entityType.MongoDb();
            }
            return(_mongoDatabase.GetCollection <TEntity>(annotations.CollectionName, annotations.CollectionSettings));
        }
Ejemplo n.º 9
0
        public void Can_write_discriminator()
        {
            var discriminator = "discriminator";
            var model         = new Model();
            var entityType    = new EntityType(typeof(RootType), model, ConfigurationSource.Explicit);
            var mongoDbEntityTypeAnnotations = new MongoDbEntityTypeAnnotations(entityType)
            {
                Discriminator = discriminator
            };

            Assert.Equal(discriminator, mongoDbEntityTypeAnnotations.Discriminator);
        }
Ejemplo n.º 10
0
        public void Can_write_collection_name()
        {
            var collectionName = "myCollection";
            var model          = new Model();
            var entityType     = new EntityType(typeof(RootType), model, ConfigurationSource.Explicit);
            var mongoDbEntityTypeAnnotations = new MongoDbEntityTypeAnnotations(entityType)
            {
                CollectionName = collectionName
            };

            Assert.Equal(collectionName, mongoDbEntityTypeAnnotations.CollectionName);
        }
Ejemplo n.º 11
0
        private void ValidateDiscriminator(IEntityType entityType, ISet <Tuple <IEntityType, string> > discriminatorSet)
        {
            var annotations = new MongoDbEntityTypeAnnotations(entityType);

            if (string.IsNullOrWhiteSpace(annotations.Discriminator))
            {
                throw new InvalidOperationException($"Missing discriminator value for entity type {entityType.DisplayName()}.");
            }
            if (!discriminatorSet.Add(Tuple.Create(entityType.RootType(), annotations.Discriminator)))
            {
                throw new InvalidOperationException($"Duplicate discriminator value {annotations.Discriminator} for root entity type {entityType.RootType().DisplayName()} (defined on {entityType.DisplayName()}).");
            }
        }
Ejemplo n.º 12
0
 public MongoDbDocumentBuilder([NotNull] InternalEntityTypeBuilder internalEntityTypeBuilder,
                               ConfigurationSource configurationSource)
 {
     Check.NotNull(internalEntityTypeBuilder, nameof(internalEntityTypeBuilder));
     if (!Enum.IsDefined(typeof(ConfigurationSource), configurationSource))
     {
         throw new ArgumentOutOfRangeException(nameof(configurationSource),
                                               $"{configurationSource} is not a valid {nameof(Microsoft.EntityFrameworkCore.Metadata.Internal.ConfigurationSource)} value.");
     }
     InternalEntityTypeBuilder    = internalEntityTypeBuilder;
     ConfigurationSource          = configurationSource;
     MongoDbEntityTypeAnnotations = new MongoDbEntityTypeAnnotations(internalEntityTypeBuilder.Metadata);
 }
        /// <inheritdoc />
        public Expression CreateDocumentQueryExpression(IEntityType entityType)
        {
            MongoDbEntityTypeAnnotations annotations = Check.NotNull(entityType, nameof(entityType)).MongoDb();

            IEntityType queryEntityType = entityType;

            if (!entityType.IsDocumentRootEntityType())
            {
                entityType = entityType.GetMongoDbCollectionEntityType();
            }

            Expression queryExpression = Expression.Call(
                Expression.Constant(_mongoDbConnection.GetDatabase()),
                GetCollectionMethodInfo.MakeGenericMethod(entityType.ClrType),
                new Expression[]
            {
                Expression.Constant(annotations.CollectionName),
                Expression.Constant(
                    annotations.CollectionSettings,
                    typeof(MongoCollectionSettings))
            });

            queryExpression = Expression.Call(
                null,
                AsQueryableMethodInfo.MakeGenericMethod(entityType.ClrType),
                new []
            {
                queryExpression,
                Expression.Constant(
                    null,
                    typeof(AggregateOptions))
            });

            if (queryEntityType != entityType)
            {
                queryExpression = Expression.Call(
                    queryExpression,
                    OfTypeMethodInfo.MakeGenericMethod(queryEntityType.ClrType));
            }

            return(queryExpression);
        }
        /// <inheritdoc />
        protected override Expression VisitEntityQueryable(Type elementType)
        {
            Check.NotNull(elementType, nameof(elementType));

            IEntityType entityType = QueryModelVisitor.QueryCompilationContext.FindEntityType(_querySource)
                                     ?? _model.FindEntityType(elementType);
            MongoDbEntityTypeAnnotations annotations = entityType.MongoDb();

            while (annotations.IsDerivedType && entityType.BaseType != null)
            {
                entityType  = entityType.BaseType;
                annotations = entityType.MongoDb();
            }

            return(Expression.Call(
                       entityType.ClrType == elementType
                    ? EntityQueryMethodInfo.MakeGenericMethod(elementType)
                    : SubEntityQueryMethodInfo.MakeGenericMethod(entityType.ClrType, elementType),
                       EntityQueryModelVisitor.QueryContextParameter));
        }
        /// <inheritdoc />
        public override InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder,
                                                        BsonDiscriminatorAttribute attribute)
        {
            Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
            Check.NotNull(attribute, nameof(attribute));
            MongoDbEntityTypeAnnotations annotations = entityTypeBuilder.MongoDb();

            if (!string.IsNullOrWhiteSpace(attribute.Discriminator))
            {
                annotations.Discriminator = attribute.Discriminator;
            }

            if (!annotations.DiscriminatorIsRequired)
            {
                annotations.DiscriminatorIsRequired = attribute.Required;
            }

            annotations.IsRootType = attribute.RootClass;
            return(entityTypeBuilder);
        }