public static ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> HasComplexAnnotations <TStructuralType, TProperty>( this StructuralTypeConfiguration <TStructuralType> configuration, Expression <Func <TStructuralType, IEnumerable <TProperty> > > propertyExpression) where TStructuralType : class where TProperty : class { Arg.NotNull(configuration, nameof(configuration)); Arg.NotNull(propertyExpression, nameof(propertyExpression)); Contract.Ensures(Contract.Result <ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> >() != null); string name; var key = propertyExpression.GetInstanceAnnotationKey(out name); var builder = configuration.GetModelBuilder(); var configurations = builder.GetAnnotationConfigurations(); ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> annotationConfig; // if the property has already been configured, return the existing configuration if (configurations.TryGet(key, out annotationConfig)) { return(annotationConfig); } var annotationType = typeof(TProperty); // ensure a nested collection hasn't been specified if (annotationType.IsEnumerable()) { throw new InvalidOperationException(string.Format(CurrentCulture, InvalidComplexTypeCollection, annotationType)); } // always ignore the annotation property from the entity model propertyExpression.IgnoredBy(configuration); // build an annotation for the entity property var accessor = propertyExpression.ToLazyContravariantFunc(); var qualifiedName = Invariant($"{configuration.Namespace}.{name}"); var annotationTypeConfig = builder.ComplexType <TProperty>(); var annotation = new InstanceAnnotation(accessor, qualifiedName, annotationTypeConfig.ToEdmTypeConfiguration()) { IsComplex = true, IsCollection = true, IsNullable = true }; annotationConfig = new ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty>(configuration, name, annotationTypeConfig, annotation); configurations.Add(key, annotationConfig); return(annotationConfig); }
private static InstanceAnnotationConfiguration <TStructuralType> HasPrimitiveAnnotations <TStructuralType, TProperty>( this StructuralTypeConfiguration <TStructuralType> configuration, Expression <Func <TStructuralType, IEnumerable <TProperty> > > propertyExpression) where TStructuralType : class { Arg.NotNull(configuration, nameof(configuration)); Arg.NotNull(propertyExpression, nameof(propertyExpression)); Contract.Ensures(Contract.Result <InstanceAnnotationConfiguration <TStructuralType> >() != null); string name; var key = propertyExpression.GetInstanceAnnotationKey(out name); var builder = configuration.GetModelBuilder(); var configurations = builder.GetAnnotationConfigurations(); InstanceAnnotationConfiguration <TStructuralType> annotationConfig; // if the property has already been configured, return the existing configuration if (configurations.TryGet(key, out annotationConfig)) { return(annotationConfig); } // always ignore the annotation property from the entity model propertyExpression.IgnoredBy(configuration); // build an annotation for the entity property var accessor = propertyExpression.ToLazyContravariantFunc(); var qualifiedName = Invariant($"{configuration.Namespace}.{name}"); var annotationType = typeof(TProperty); var annotationTypeConfig = builder.GetTypeConfigurationOrNull(annotationType); var annotation = new InstanceAnnotation(accessor, qualifiedName, annotationTypeConfig) { IsCollection = true, IsNullable = annotationType.IsNullable() }; annotationConfig = new InstanceAnnotationConfiguration <TStructuralType>(configuration, name, annotation); configurations.Add(key, annotationConfig); return(annotationConfig); }