public void Map(PropertyInfo propertyInfo, ICollection <MetadataProperty> annotations) { DebugCheck.NotNull(propertyInfo); DebugCheck.NotNull(annotations); annotations.SetClrAttributes(_attributeProvider.GetAttributes(propertyInfo).ToList()); }
/// <summary> /// Creates and configures the <see cref="DbModelBuilder" /> instance that will be used to build the /// <see cref="DbCompiledModel" />. /// </summary> /// <returns> The builder. </returns> public DbModelBuilder CreateModelBuilder() { var versionAttribute = _attributeProvider.GetAttributes(Owner.GetType()) .OfType <DbModelBuilderVersionAttribute>() .FirstOrDefault(); var version = versionAttribute != null ? versionAttribute.Version : DbModelBuilderVersion.Latest; var modelBuilder = new DbModelBuilder(version); var modelNamespace = StripInvalidCharacters(Owner.GetType().Namespace); if (!String.IsNullOrWhiteSpace(modelNamespace)) { modelBuilder.Conventions.Add(new ModelNamespaceConvention(modelNamespace)); } var modelContainer = StripInvalidCharacters(Owner.GetType().Name); if (!String.IsNullOrWhiteSpace(modelContainer)) { modelBuilder.Conventions.Add(new ModelContainerConvention(modelContainer)); } new DbSetDiscoveryService(Owner).RegisterSets(modelBuilder); Owner.CallOnModelCreating(modelBuilder); if (OnModelCreating != null) { OnModelCreating(modelBuilder); } return(modelBuilder); }
/// <summary> /// Initializes a new instance of the <see cref="TypeAttributeConfigurationConvention{TAttribute}"/> class. /// </summary> protected TypeAttributeConfigurationConvention() { Types().Having(t => _attributeProvider.GetAttributes(t).OfType <TAttribute>()) .Configure((configuration, attributes) => { foreach (var attribute in attributes) { Apply(configuration, attribute); } }); }
/// <summary> /// Initializes a new instance of the <see cref="PrimitivePropertyAttributeConfigurationConvention{TAttribute}"/> class. /// </summary> protected PrimitivePropertyAttributeConfigurationConvention() { Properties().Having(pi => _attributeProvider.GetAttributes(pi).OfType <TAttribute>()).Configure( (configuration, attributes) => { foreach (var attribute in attributes) { Apply(configuration, attribute); } }); }
void IConfigurationConvention <TMemberInfo, TConfiguration> .Apply( TMemberInfo memberInfo, Func <TConfiguration> configuration) { Check.NotNull(memberInfo, "memberInfo"); Check.NotNull(configuration, "configuration"); foreach (var attribute in _attributeProvider.GetAttributes(memberInfo).OfType <TAttribute>()) { Apply(memberInfo, configuration(), attribute); } }
/// <summary> /// Initializes a new instance of the <see cref="PropertyAttributeConfigurationConvention{TAttribute}"/> class. /// </summary> protected PropertyAttributeConfigurationConvention() { Types().Configure( ec => { foreach (var propertyInfo in ec.ClrType.GetProperties(PropertyFilter.DefaultBindingFlags)) { foreach (var attribute in _attributeProvider.GetAttributes(propertyInfo).OfType <TAttribute>()) { Apply(propertyInfo, ec, attribute); } } }); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyAttributeConfigurationConvention{TAttribute}"/> class. /// </summary> protected PropertyAttributeConfigurationConvention() { Types().Configure( ec => { foreach (var propertyInfo in ec.ClrType.GetInstanceProperties()) { foreach (var attribute in _attributeProvider.GetAttributes(propertyInfo).OfType <TAttribute>()) { Apply(propertyInfo, ec, attribute); } } }); }
/// <summary> /// Extracted method from BuildEntityValidator and BuildComplexTypeValidator /// </summary> private T BuildTypeValidator <T>( Type clrType, IEnumerable <EdmProperty> edmProperties, IEnumerable <NavigationProperty> navigationProperties, Func <IEnumerable <PropertyValidator>, IEnumerable <IValidator>, T> validatorFactoryFunc) where T : TypeValidator { var propertyValidators = BuildValidatorsForProperties( GetPublicInstanceProperties(clrType), edmProperties, navigationProperties); var attributes = _attributeProvider.GetAttributes(clrType); var typeLevelValidators = BuildValidationAttributeValidators(attributes); if (typeof(IValidatableObject).IsAssignableFrom(clrType)) { typeLevelValidators.Add( new ValidatableObjectValidator(attributes.OfType <DisplayAttribute>().SingleOrDefault())); } return(propertyValidators.Any() || typeLevelValidators.Any() ? validatorFactoryFunc(propertyValidators, typeLevelValidators) : null); }
// Not using the public API to avoid including the property in the model if it wasn't in before internal override void ApplyPropertyTypeConfiguration <TStructuralTypeConfiguration>( PropertyInfo propertyInfo, Func <TStructuralTypeConfiguration> structuralTypeConfiguration, ModelConfiguration modelConfiguration) { DebugCheck.NotNull(propertyInfo); DebugCheck.NotNull(structuralTypeConfiguration); DebugCheck.NotNull(modelConfiguration); if (typeof(TStructuralTypeConfiguration) == typeof(EntityTypeConfiguration) && _attributeProvider.GetAttributes(propertyInfo).OfType <KeyAttribute>().Any()) { var entityTypeConfiguration = (EntityTypeConfiguration)(object)structuralTypeConfiguration(); if (propertyInfo.IsValidEdmScalarProperty()) { entityTypeConfiguration.Key(propertyInfo); } } }
// Not using the public API to avoid configuring the property as a navigation property if it wasn't one before internal override void ApplyPropertyConfiguration( PropertyInfo propertyInfo, Func<PropertyConfiguration> propertyConfiguration, ModelConfiguration modelConfiguration) { DebugCheck.NotNull(propertyInfo); DebugCheck.NotNull(propertyConfiguration); DebugCheck.NotNull(modelConfiguration); if (propertyInfo.IsValidEdmNavigationProperty() && !propertyInfo.PropertyType.IsCollection() && _attributeProvider.GetAttributes(propertyInfo).OfType<RequiredAttribute>().Any()) { var navigationPropertyConfiguration = (NavigationPropertyConfiguration)propertyConfiguration(); if (navigationPropertyConfiguration.RelationshipMultiplicity == null) { navigationPropertyConfiguration.RelationshipMultiplicity = (RelationshipMultiplicity.One); } } }
/// <summary> /// Initializes a new instance of the <see cref="PropertyAttributeConfigurationConvention{TAttribute}"/> class. /// </summary> protected PropertyAttributeConfigurationConvention() { Types().Configure( ec => { // PERF: this code is part of a critical section, consider its performance when refactoring foreach (var propertyInfo in ec.ClrType.GetInstanceProperties()) { var attributes = (IList <Attribute>)_attributeProvider.GetAttributes(propertyInfo); // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < attributes.Count; ++i) { var attribute = attributes[i] as TAttribute; if (attribute != null) { Apply(propertyInfo, ec, attribute); } } } }); }
public AttributeToTableAnnotationConvention( string annotationName, Func <Type, IList <TAttribute>, TAnnotation> annotationFactory) { Check.NotEmpty(annotationName, nameof(annotationName)); Check.NotNull <Func <Type, IList <TAttribute>, TAnnotation> >(annotationFactory, nameof(annotationFactory)); AttributeProvider attributeProvider = DbConfiguration.DependencyResolver.GetService <AttributeProvider>(); this.Types().Having <List <TAttribute> >((Func <Type, List <TAttribute> >)(t => attributeProvider.GetAttributes(t).OfType <TAttribute>().ToList <TAttribute>())).Configure((Action <ConventionTypeConfiguration, List <TAttribute> >)((c, a) => { if (!a.Any <TAttribute>()) { return; } c.HasTableAnnotation(annotationName, (object)annotationFactory(c.ClrType, (IList <TAttribute>)a)); })); }
public AttributeToColumnAnnotationConvention( string annotationName, Func <PropertyInfo, IList <TAttribute>, TAnnotation> annotationFactory) { Check.NotEmpty(annotationName, nameof(annotationName)); Check.NotNull <Func <PropertyInfo, IList <TAttribute>, TAnnotation> >(annotationFactory, nameof(annotationFactory)); AttributeProvider attributeProvider = DbConfiguration.DependencyResolver.GetService <AttributeProvider>(); this.Properties().Having <List <TAttribute> >((Func <PropertyInfo, List <TAttribute> >)(pi => attributeProvider.GetAttributes(pi).OfType <TAttribute>().ToList <TAttribute>())).Configure((Action <ConventionPrimitivePropertyConfiguration, List <TAttribute> >)((c, a) => { if (!a.Any <TAttribute>()) { return; } c.HasColumnAnnotation(annotationName, (object)annotationFactory(c.ClrPropertyInfo, (IList <TAttribute>)a)); })); }