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 void GenerateIndependentAssociationType( AssociationType associationType, DbDatabaseMapping databaseMapping) { DebugCheck.NotNull(associationType); DebugCheck.NotNull(databaseMapping); AssociationEndMember principalEnd, dependentEnd; if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd)) { if (!associationType.IsPrincipalConfigured()) { throw Error.UnableToDeterminePrincipal( associationType.SourceEnd.GetEntityType().GetClrType(), associationType.TargetEnd.GetEntityType().GetClrType()); } principalEnd = associationType.SourceEnd; dependentEnd = associationType.TargetEnd; } var dependentEntityTypeMapping = GetEntityTypeMappingInHierarchy(databaseMapping, dependentEnd.GetEntityType()); var dependentTable = dependentEntityTypeMapping .MappingFragments .First() .Table; var associationSetMapping = GenerateAssociationSetMapping( associationType, databaseMapping, principalEnd, dependentEnd, dependentTable); GenerateIndependentForeignKeyConstraint( databaseMapping, principalEnd.GetEntityType(), dependentEnd.GetEntityType(), dependentTable, associationSetMapping, associationSetMapping.SourceEndMapping, associationType.Name, principalEnd); foreach (var property in dependentEnd.GetEntityType().KeyProperties()) { associationSetMapping.TargetEndMapping .AddProperty( new StorageScalarPropertyMapping( property, dependentEntityTypeMapping.GetPropertyMapping(property).ColumnProperty)); } }
/// <inheritdoc /> public virtual void Apply(NavigationProperty item, DbModel model) { Check.NotNull <NavigationProperty>(item, nameof(item)); Check.NotNull <DbModel>(model, nameof(model)); AssociationType association = item.Association; if (association.Constraint != null) { return; } ForeignKeyAttribute foreignKeyAttribute = item.GetClrAttributes <ForeignKeyAttribute>().SingleOrDefault <ForeignKeyAttribute>(); AssociationEndMember principalEnd; AssociationEndMember dependentEnd; if (foreignKeyAttribute == null || !association.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd) && !association.IsPrincipalConfigured()) { return; } AssociationEndMember associationEndMember = dependentEnd ?? association.TargetEnd; principalEnd = principalEnd ?? association.SourceEnd; IEnumerable <string> dependentPropertyNames = ((IEnumerable <string>)foreignKeyAttribute.Name.Split(',')).Select <string, string>((Func <string, string>)(p => p.Trim())); EntityType declaringEntityType = model.ConceptualModel.EntityTypes.Single <EntityType>((Func <EntityType, bool>)(e => e.DeclaredNavigationProperties.Contains(item))); List <EdmProperty> list = ForeignKeyNavigationPropertyAttributeConvention.GetDependentProperties(associationEndMember.GetEntityType(), dependentPropertyNames, declaringEntityType, item).ToList <EdmProperty>(); ReferentialConstraint constraint = new ReferentialConstraint((RelationshipEndMember)principalEnd, (RelationshipEndMember)associationEndMember, (IEnumerable <EdmProperty>)principalEnd.GetEntityType().KeyProperties().ToList <EdmProperty>(), (IEnumerable <EdmProperty>)list); IEnumerable <EdmProperty> source = associationEndMember.GetEntityType().KeyProperties(); if (source.Count <EdmProperty>() == constraint.ToProperties.Count <EdmProperty>() && source.All <EdmProperty>((Func <EdmProperty, bool>)(kp => constraint.ToProperties.Contains(kp)))) { principalEnd.RelationshipMultiplicity = RelationshipMultiplicity.One; if (associationEndMember.RelationshipMultiplicity.IsMany()) { associationEndMember.RelationshipMultiplicity = RelationshipMultiplicity.ZeroOrOne; } } if (principalEnd.IsRequired()) { constraint.ToProperties.Each <EdmProperty, bool>((Func <EdmProperty, bool>)(p => p.Nullable = false)); } association.Constraint = constraint; }