private void GenerateIndependentAssociationType( AssociationType associationType, DbDatabaseMapping databaseMapping) { AssociationEndMember principalEnd; AssociationEndMember dependentEnd; if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd)) { if (!associationType.IsPrincipalConfigured()) { throw Error.UnableToDeterminePrincipal((object)EntityTypeExtensions.GetClrType(associationType.SourceEnd.GetEntityType()), (object)EntityTypeExtensions.GetClrType(associationType.TargetEnd.GetEntityType())); } principalEnd = associationType.SourceEnd; dependentEnd = associationType.TargetEnd; } EntityTypeMapping mappingInHierarchy = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType()); EntityType table = mappingInHierarchy.MappingFragments.First <MappingFragment>().Table; AssociationSetMapping associationSetMapping = AssociationTypeMappingGenerator.GenerateAssociationSetMapping(associationType, databaseMapping, principalEnd, dependentEnd, table); this.GenerateIndependentForeignKeyConstraint(databaseMapping, principalEnd.GetEntityType(), dependentEnd.GetEntityType(), table, associationSetMapping, associationSetMapping.SourceEndMapping, associationType.Name, principalEnd, false); foreach (EdmProperty keyProperty in dependentEnd.GetEntityType().KeyProperties()) { associationSetMapping.TargetEndMapping.AddPropertyMapping(new ScalarPropertyMapping(keyProperty, mappingInHierarchy.GetPropertyMapping(keyProperty).ColumnProperty)); } }
private static void GenerateForeignKeyAssociationType( AssociationType associationType, DbDatabaseMapping databaseMapping) { AssociationEndMember dependentEnd = associationType.Constraint.DependentEnd; AssociationEndMember otherEnd = associationType.GetOtherEnd(dependentEnd); EntityTypeMapping mappingInHierarchy = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, otherEnd.GetEntityType()); EntityTypeMapping dependentEntityTypeMapping = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType()); ForeignKeyBuilder foreignKeyBuilder = new ForeignKeyBuilder(databaseMapping.Database, associationType.Name) { PrincipalTable = mappingInHierarchy.MappingFragments.Single <MappingFragment>().Table, DeleteAction = otherEnd.DeleteBehavior != OperationAction.None ? otherEnd.DeleteBehavior : OperationAction.None }; dependentEntityTypeMapping.MappingFragments.Single <MappingFragment>().Table.AddForeignKey(foreignKeyBuilder); foreignKeyBuilder.DependentColumns = associationType.Constraint.ToProperties.Select <EdmProperty, EdmProperty>((Func <EdmProperty, EdmProperty>)(dependentProperty => dependentEntityTypeMapping.GetPropertyMapping(dependentProperty).ColumnProperty)); foreignKeyBuilder.SetAssociationType(associationType); }
internal static void MapPrimitivePropertyFacets( EdmProperty property, EdmProperty column, TypeUsage typeUsage) { if (StructuralTypeMappingGenerator.IsValidFacet(typeUsage, "FixedLength") && property.IsFixedLength.HasValue) { column.IsFixedLength = property.IsFixedLength; } if (StructuralTypeMappingGenerator.IsValidFacet(typeUsage, "MaxLength")) { column.IsMaxLength = property.IsMaxLength; if (!column.IsMaxLength || property.MaxLength.HasValue) { column.MaxLength = property.MaxLength; } } if (StructuralTypeMappingGenerator.IsValidFacet(typeUsage, "Unicode") && property.IsUnicode.HasValue) { column.IsUnicode = property.IsUnicode; } if (StructuralTypeMappingGenerator.IsValidFacet(typeUsage, "Precision")) { byte?precision = property.Precision; if ((precision.HasValue ? new int?((int)precision.GetValueOrDefault()) : new int?()).HasValue) { column.Precision = property.Precision; } } if (!StructuralTypeMappingGenerator.IsValidFacet(typeUsage, "Scale")) { return; } byte?scale = property.Scale; if (!(scale.HasValue ? new int?((int)scale.GetValueOrDefault()) : new int?()).HasValue) { return; } column.Scale = property.Scale; }
private void GenerateIndependentForeignKeyConstraint( DbDatabaseMapping databaseMapping, EntityType principalEntityType, EntityType dependentEntityType, EntityType dependentTable, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, string name, AssociationEndMember principalEnd, bool isPrimaryKeyColumn = false) { EntityType table = StructuralTypeMappingGenerator.GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType).MappingFragments.Single <MappingFragment>().Table; ForeignKeyBuilder foreignKeyBuilder = new ForeignKeyBuilder(databaseMapping.Database, name) { PrincipalTable = table, DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None ? associationEndMapping.AssociationEnd.DeleteBehavior : OperationAction.None }; NavigationProperty principalNavigationProperty = databaseMapping.Model.EntityTypes.SelectMany <EntityType, NavigationProperty>((Func <EntityType, IEnumerable <NavigationProperty> >)(e => (IEnumerable <NavigationProperty>)e.DeclaredNavigationProperties)).SingleOrDefault <NavigationProperty>((Func <NavigationProperty, bool>)(n => n.ResultEnd == principalEnd)); dependentTable.AddForeignKey(foreignKeyBuilder); foreignKeyBuilder.DependentColumns = this.GenerateIndependentForeignKeyColumns(principalEntityType, dependentEntityType, associationSetMapping, associationEndMapping, dependentTable, isPrimaryKeyColumn, principalNavigationProperty); }
protected EdmProperty MapTableColumn( EdmProperty property, string columnName, bool isInstancePropertyOnDerivedType) { TypeUsage storeType = this._providerManifest.GetStoreType(TypeUsage.Create((EdmType)property.UnderlyingPrimitiveType, (IEnumerable <Facet>)property.TypeUsage.Facets)); EdmProperty column = new EdmProperty(columnName, storeType) { Nullable = isInstancePropertyOnDerivedType || property.Nullable }; if (column.IsPrimaryKeyColumn) { column.Nullable = false; } StoreGeneratedPattern?generatedPattern = property.GetStoreGeneratedPattern(); if (generatedPattern.HasValue) { column.StoreGeneratedPattern = generatedPattern.Value; } StructuralTypeMappingGenerator.MapPrimitivePropertyFacets(property, column, storeType); return(column); }