Esempio n. 1
0
        public static EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> HasComplexAnnotations <TEntity, TStructuralType>(
            this EntitySetConfiguration <TEntity> configuration,
            string name,
            IEnumerable <TStructuralType> complexValues)
            where TEntity : class
            where TStructuralType : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNullOrEmpty(name, nameof(name));
            Contract.Ensures(Contract.Result <EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> >() != null);

            var entitySetName  = configuration.GetEntitySetName();
            var key            = Invariant($"EntitySet_{entitySetName}_{name}");
            var builder        = configuration.EntityType.GetModelBuilder();
            var configurations = builder.GetAnnotationConfigurations();
            EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            var annotationType = typeof(TStructuralType);

            // ensure this isn't a collection; must use HasAnnotations instead
            if (annotationType.IsEnumerable())
            {
                throw new InvalidOperationException(string.Format(CurrentCulture, InvalidComplexType, annotationType));
            }

            // build an annotation for the entity property
            var entityType           = configuration.EntityType;
            var qualifiedName        = Invariant($"{entityType.Namespace}.{name}");
            var annotationTypeConfig = builder.ComplexType <TStructuralType>();
            var annotation           = new InstanceAnnotation(o => complexValues, qualifiedName, annotationTypeConfig.ToEdmTypeConfiguration())
            {
                IsComplex    = true,
                IsCollection = true,
                IsNullable   = true
            };

            annotationConfig = new EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType>(entitySetName, entityType.ToEdmTypeConfiguration(), name, annotationTypeConfig, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }
Esempio n. 2
0
        private static EntitySetInstanceAnnotationConfiguration HasPrimitiveAnnotations <TEntity, TValue>(
            this EntitySetConfiguration <TEntity> configuration,
            string name,
            IEnumerable <TValue> values)
            where TEntity : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNullOrEmpty(name, nameof(name));
            Contract.Ensures(Contract.Result <EntitySetInstanceAnnotationConfiguration>() != null);

            var entitySetName  = configuration.GetEntitySetName();
            var key            = Invariant($"EntitySet_{entitySetName}_{name}");
            var builder        = configuration.EntityType.GetModelBuilder();
            var configurations = builder.GetAnnotationConfigurations();
            EntitySetInstanceAnnotationConfiguration annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            var entityType           = configuration.EntityType;
            var qualifiedName        = Invariant($"{entityType.Namespace}.{name}");
            var annotationType       = typeof(TValue);
            var annotationTypeConfig = builder.GetTypeConfigurationOrNull(annotationType);
            var annotation           = new InstanceAnnotation(o => values, qualifiedName, annotationTypeConfig)
            {
                IsCollection = true,
                IsNullable   = annotationType.IsNullable()
            };

            annotationConfig = new EntitySetInstanceAnnotationConfiguration(entitySetName, entityType.ToEdmTypeConfiguration(), name, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }