public EntityType MapEntityType(Type type) { if (!type.IsValidStructuralType() || this._mappingContext.ModelConfiguration.IsIgnoredType(type) || this._mappingContext.ModelConfiguration.IsComplexType(type)) { return((EntityType)null); } EntityType entityType = TypeMapper.GetExistingEdmType <EntityType>(this._mappingContext.Model, type); if (entityType == null) { this._mappingContext.ConventionsConfiguration.ApplyModelConfiguration(type, this._mappingContext.ModelConfiguration); if (this._mappingContext.ModelConfiguration.IsIgnoredType(type) || this._mappingContext.ModelConfiguration.IsComplexType(type)) { return((EntityType)null); } entityType = this._mappingContext.Model.AddEntityType(type.Name, this._mappingContext.ModelConfiguration.ModelNamespace); entityType.Abstract = type.IsAbstract(); EntityType entityType1 = this._mappingContext.Model.GetEntityType(type.BaseType().Name); if (entityType1 == null) { this._mappingContext.Model.AddEntitySet(entityType.Name, entityType, (string)null); } else if (object.ReferenceEquals((object)entityType1, (object)entityType)) { throw new NotSupportedException(Strings.SimpleNameCollision((object)type.FullName, (object)type.BaseType().FullName, (object)type.Name)); } entityType.BaseType = (EdmType)entityType1; Func <EntityTypeConfiguration> entityTypeConfiguration = (Func <EntityTypeConfiguration>)(() => this._mappingContext.ModelConfiguration.Entity(type)); this._mappingContext.ConventionsConfiguration.ApplyTypeConfiguration <EntityTypeConfiguration>(type, entityTypeConfiguration, this._mappingContext.ModelConfiguration); List <PropertyInfo> navigationProperties = new List <PropertyInfo>(); this.MapStructuralElements <EntityTypeConfiguration>(type, (ICollection <MetadataProperty>)entityType.GetMetadataProperties(), (Action <PropertyMapper, PropertyInfo>)((m, p) => { if (m.MapIfNotNavigationProperty(p, entityType, entityTypeConfiguration)) { return; } navigationProperties.Add(p); }), entityTypeConfiguration); IEnumerable <PropertyInfo> source = (IEnumerable <PropertyInfo>)navigationProperties; if (this._mappingContext.ModelBuilderVersion.IsEF6OrHigher()) { source = (IEnumerable <PropertyInfo>)source.OrderBy <PropertyInfo, string>((Func <PropertyInfo, string>)(p => p.Name)); } foreach (PropertyInfo propertyInfo in source) { new NavigationPropertyMapper(this).Map(propertyInfo, entityType, entityTypeConfiguration); } if (entityType.BaseType != null) { this.LiftInheritedProperties(type, entityType); } this.MapDerivedTypes(type, entityType); } return(entityType); }
public ComplexType MapComplexType(Type type, bool discoverNested = false) { if (!type.IsValidStructuralType()) { return((ComplexType)null); } this._mappingContext.ConventionsConfiguration.ApplyModelConfiguration(type, this._mappingContext.ModelConfiguration); if (this._mappingContext.ModelConfiguration.IsIgnoredType(type) || !discoverNested && !this._mappingContext.ModelConfiguration.IsComplexType(type)) { return((ComplexType)null); } ComplexType complexType = TypeMapper.GetExistingEdmType <ComplexType>(this._mappingContext.Model, type); if (complexType == null) { complexType = this._mappingContext.Model.AddComplexType(type.Name, this._mappingContext.ModelConfiguration.ModelNamespace); Func <ComplexTypeConfiguration> complexTypeConfiguration = (Func <ComplexTypeConfiguration>)(() => this._mappingContext.ModelConfiguration.ComplexType(type)); this._mappingContext.ConventionsConfiguration.ApplyTypeConfiguration <ComplexTypeConfiguration>(type, complexTypeConfiguration, this._mappingContext.ModelConfiguration); this.MapStructuralElements <ComplexTypeConfiguration>(type, (ICollection <MetadataProperty>)complexType.GetMetadataProperties(), (Action <PropertyMapper, PropertyInfo>)((m, p) => m.Map(p, complexType, complexTypeConfiguration)), complexTypeConfiguration); } return(complexType); }
public EnumType MapEnumType(Type type) { EnumType enumType = TypeMapper.GetExistingEdmType <EnumType>(this._mappingContext.Model, type); if (enumType == null) { PrimitiveType primitiveType; if (!Enum.GetUnderlyingType(type).IsPrimitiveType(out primitiveType)) { return((EnumType)null); } enumType = this._mappingContext.Model.AddEnumType(type.Name, this._mappingContext.ModelConfiguration.ModelNamespace); enumType.IsFlags = type.GetCustomAttributes <FlagsAttribute>(false).Any <FlagsAttribute>(); enumType.SetClrType(type); enumType.UnderlyingType = primitiveType; foreach (string name in Enum.GetNames(type)) { enumType.AddMember(new EnumMember(name, Convert.ChangeType(Enum.Parse(type, name), type.GetEnumUnderlyingType(), (IFormatProvider)CultureInfo.InvariantCulture))); } } return(enumType); }