|
780 | 780 | AddRelationship( fkList, tablesAndViews, name, pkSchema, pkTable, new String[] { pkColumn }, fkSchema, fkTable, new String[] { fkColumn } ); |
781 | 781 | } |
782 | 782 |
|
783 | | - public static void AddRelationship(List<ForeignKey> fkList, Tables tablesAndViews, String relationshipName, String pkSchema, String pkTableName, String[] pkColumns, String fkSchema, String fkTableName, String[] fkColumns) |
| 783 | + public static void AddRelationship(List<ForeignKey> fkList, Tables tablesAndViews, String relationshipName, String pkSchema, String pkTableName, String[] pkColumns, String fkSchema, String fkTableName, String[] fkColumns, Boolean includeReverseNavigation = true) |
784 | 784 | { |
785 | 785 | // Argument validation: |
786 | 786 | if( fkList == null ) throw new ArgumentNullException("fkList"); |
|
789 | 789 | if( string.IsNullOrEmpty(pkSchema) ) throw new ArgumentNullException("pkSchema"); |
790 | 790 | if( string.IsNullOrEmpty(pkTableName) ) throw new ArgumentNullException("pkTableName"); |
791 | 791 | if( pkColumns == null ) throw new ArgumentNullException("pkColumns"); |
792 | | - if( pkColumns.Length == 0 || pkColumns.Any( s => string.IsNullOrEmpty(s) ) ) throw new ArgumentException("pkColumns"); |
| 792 | + if( pkColumns.Length == 0 || pkColumns.Any( s => string.IsNullOrEmpty(s) ) ) throw new ArgumentException( "Invalid primary-key columns: No primary-key column names are specified, or at least one primary-key column name is empty.", "pkColumns" ); |
793 | 793 | if( string.IsNullOrEmpty(fkSchema) ) throw new ArgumentNullException("fkSchema"); |
794 | 794 | if( string.IsNullOrEmpty(fkTableName) ) throw new ArgumentNullException("fkTableName"); |
795 | 795 | if( fkColumns == null ) throw new ArgumentNullException("fkColumns"); |
796 | | - if( fkColumns.Length != pkColumns.Length || fkColumns.Any( s => string.IsNullOrEmpty(s) ) ) throw new ArgumentException("fkColumns"); |
| 796 | + if( fkColumns.Length != pkColumns.Length || fkColumns.Any( s => string.IsNullOrEmpty(s) ) ) throw new ArgumentException("Invalid foreign-key columns:Foreign-key column list has a different number of columns than the primary-key column list, or at least one foreign-key column name is empty.", "pkColumns" ); |
797 | 797 |
|
798 | 798 | ////////////////// |
799 | 799 |
|
800 | 800 | Table pkTable = tablesAndViews.GetTable(pkTableName, pkSchema); |
| 801 | + if( pkTable == null ) throw new ArgumentException( "Couldn't find table " + pkSchema + "." + pkTableName ); |
| 802 | + |
801 | 803 | Table fkTable = tablesAndViews.GetTable(fkTableName, fkSchema); |
| 804 | + if( fkTable == null ) throw new ArgumentException( "Couldn't find table " + fkSchema + "." + fkTableName ); |
| 805 | + |
| 806 | + // Ensure all columns exist: |
| 807 | + foreach( String pkCol in pkColumns ) |
| 808 | + { |
| 809 | + if( pkTable.Columns.SingleOrDefault( c => c.Name == pkCol ) == null ) throw new ArgumentException( "The relationship primary-key column \"" + pkCol + "\" does not exist in table or view " + pkSchema + "." + pkTableName ); |
| 810 | + } |
| 811 | + foreach( String fkCol in fkColumns ) |
| 812 | + { |
| 813 | + if( fkTable.Columns.SingleOrDefault( c => c.Name == fkCol ) == null ) throw new ArgumentException( "The relationship foreign-key column \"" + fkCol + "\" does not exist in table or view " + fkSchema + "." + fkTableName ); |
| 814 | + } |
802 | 815 |
|
803 | 816 | for( int i = 0; i < pkColumns.Length; i++ ) |
804 | 817 | { |
|
820 | 833 | cascadeOnDelete : false, |
821 | 834 | isNotEnforced : false |
822 | 835 | ); |
823 | | - fk.IncludeReverseNavigation = true; |
| 836 | + fk.IncludeReverseNavigation = includeReverseNavigation; |
824 | 837 |
|
825 | 838 | fkList.Add( fk ); |
826 | 839 | fkTable.HasForeignKey = true; |
|
0 commit comments