public void Can_add_and_remove_foreign_key_builders() { var entityType = new EntityType("E", "N", DataSpace.CSpace); var mockForeignKeyBuilder = new Mock<ForeignKeyBuilder>(); entityType.AddForeignKey(mockForeignKeyBuilder.Object); Assert.Same(mockForeignKeyBuilder.Object, entityType.ForeignKeyBuilders.Single()); mockForeignKeyBuilder.Verify(fk => fk.SetOwner(entityType)); entityType.RemoveForeignKey(mockForeignKeyBuilder.Object); mockForeignKeyBuilder.Verify(fk => fk.SetOwner(null)); }
public void Can_add_and_remove_foreign_key_builders() { var entityType = new EntityType(); var mockForeignKeyBuilder = new Mock <ForeignKeyBuilder>(); entityType.AddForeignKey(mockForeignKeyBuilder.Object); Assert.Same(mockForeignKeyBuilder.Object, entityType.ForeignKeyBuilders.Single()); mockForeignKeyBuilder.Verify(fk => fk.SetOwner(entityType)); entityType.RemoveForeignKey(mockForeignKeyBuilder.Object); mockForeignKeyBuilder.Verify(fk => fk.SetOwner(null)); }
public static void AddTypeConstraint( EdmModel database, EntityType entityType, EntityType principalTable, EntityType dependentTable, bool isSplitting) { DebugCheck.NotNull(principalTable); DebugCheck.NotNull(dependentTable); DebugCheck.NotNull(entityType); var foreignKeyConstraintMetadata = new ForeignKeyBuilder( database, String.Format( CultureInfo.InvariantCulture, "{0}_TypeConstraint_From_{1}_To_{2}", entityType.Name, principalTable.Name, dependentTable.Name)) { PrincipalTable = principalTable }; dependentTable.AddForeignKey(foreignKeyConstraintMetadata); if (isSplitting) { foreignKeyConstraintMetadata.SetIsSplitConstraint(); } else { foreignKeyConstraintMetadata.SetIsTypeConstraint(); } foreignKeyConstraintMetadata.DependentColumns = dependentTable.Properties.Where(c => c.IsPrimaryKeyColumn); //If "DbStoreGeneratedPattern.Identity" was copied from the parent table, it should be removed dependentTable.Properties.Where(c => c.IsPrimaryKeyColumn).Each(c => c.RemoveStoreGeneratedIdentityPattern()); }
private static void CopyForeignKeyConstraint(EdmModel database, EntityType toTable, ForeignKeyBuilder fk, Func<EdmProperty, EdmProperty> selector = null) { DebugCheck.NotNull(toTable); DebugCheck.NotNull(fk); var newFk = new ForeignKeyBuilder( database, database.EntityTypes.SelectMany(t => t.ForeignKeyBuilders).UniquifyName(fk.Name)) { PrincipalTable = fk.PrincipalTable, DeleteAction = fk.DeleteAction }; newFk.SetPreferredName(fk.Name); var dependentColumns = GetDependentColumns( selector != null ? fk.DependentColumns.Select(selector) : fk.DependentColumns, toTable.Properties); if (!ContainsEquivalentForeignKey(toTable, newFk.PrincipalTable, dependentColumns)) { toTable.AddForeignKey(newFk); newFk.DependentColumns = dependentColumns; } }
// <summary> // Moves a foreign key constraint from oldTable to newTable and updates column references // </summary> private static void MoveForeignKeyConstraint( EntityType fromTable, EntityType toTable, ForeignKeyBuilder fk) { DebugCheck.NotNull(fromTable); DebugCheck.NotNull(toTable); DebugCheck.NotNull(fk); fromTable.RemoveForeignKey(fk); // Only move it to the new table if the destination is not the principal table or if all dependent columns are not FKs // Otherwise you end up with an FK from the PKs to the PKs of the same table if (fk.PrincipalTable != toTable || !fk.DependentColumns.All(c => c.IsPrimaryKeyColumn)) { // Make sure all the dependent columns refer to columns in the newTable var oldColumns = fk.DependentColumns.ToArray(); var dependentColumns = GetDependentColumns(oldColumns, toTable.Properties); if (!ContainsEquivalentForeignKey(toTable, fk.PrincipalTable, dependentColumns)) { toTable.AddForeignKey(fk); fk.DependentColumns = dependentColumns; } } }
private void GenerateIndependentForeignKeyConstraint( DbDatabaseMapping databaseMapping, EntityType principalEntityType, EntityType dependentEntityType, EntityType dependentTable, AssociationSetMapping associationSetMapping, EndPropertyMapping associationEndMapping, string name, AssociationEndMember principalEnd, bool isPrimaryKeyColumn = false) { DebugCheck.NotNull(databaseMapping); DebugCheck.NotNull(principalEntityType); DebugCheck.NotNull(dependentTable); DebugCheck.NotNull(associationEndMapping); DebugCheck.NotEmpty(name); var principalTable = GetEntityTypeMappingInHierarchy(databaseMapping, principalEntityType) .MappingFragments .Single() .Table; var foreignKeyConstraint = new ForeignKeyBuilder(databaseMapping.Database, name) { PrincipalTable = principalTable, DeleteAction = associationEndMapping.AssociationEnd.DeleteBehavior != OperationAction.None ? associationEndMapping.AssociationEnd.DeleteBehavior : OperationAction.None }; var principalNavigationProperty = databaseMapping.Model.EntityTypes .SelectMany(e => e.DeclaredNavigationProperties) .SingleOrDefault(n => n.ResultEnd == principalEnd); dependentTable.AddForeignKey(foreignKeyConstraint); foreignKeyConstraint.DependentColumns = GenerateIndependentForeignKeyColumns( principalEntityType, dependentEntityType, associationSetMapping, associationEndMapping, dependentTable, isPrimaryKeyColumn, principalNavigationProperty); }
private static AssociationSetMapping CreateIAMapping(EdmModel database, EntityType dependentTable) { var fkColumn = new EdmProperty( "FK", ProviderRegistry.Sql2008_ProviderManifest.GetStoreType( TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)))); dependentTable.AddColumn(fkColumn); var foreignKeyConstraint = new ForeignKeyBuilder(database, "FK") { PrincipalTable = database.AddTable("P") }; dependentTable.AddForeignKey(foreignKeyConstraint); foreignKeyConstraint.DependentColumns = new[] { fkColumn }; var associationSetMapping = new AssociationSetMapping( new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace)), database.GetEntitySet(dependentTable)).Initialize(); associationSetMapping.SourceEndMapping.AddPropertyMapping(new ScalarPropertyMapping(new EdmProperty("PK"), fkColumn)); return associationSetMapping; }