1 // Licensed to the .NET Foundation under one or more agreements. 2 // See the LICENSE file in the project root for more information. 3 4 // Copyright (c) 2004 Mainsoft Co. 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining 7 // a copy of this software and associated documentation files (the 8 // "Software"), to deal in the Software without restriction, including 9 // without limitation the rights to use, copy, modify, merge, publish, 10 // distribute, sublicense, and/or sell copies of the Software, and to 11 // permit persons to whom the Software is furnished to do so, subject to 12 // the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be 15 // included in all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 // 25 26 using Xunit; 27 28 29 namespace System.Data.Tests 30 { 31 public class ForeignKeyConstraintTest2 32 { 33 [Fact] Columns()34 public void Columns() 35 { 36 //int RowCount; 37 var ds = new DataSet(); 38 DataTable dtParent = DataProvider.CreateParentDataTable(); 39 DataTable dtChild = DataProvider.CreateChildDataTable(); 40 ds.Tables.Add(dtParent); 41 ds.Tables.Add(dtChild); 42 43 ForeignKeyConstraint fc = null; 44 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 45 46 // Columns 47 Assert.Equal(dtChild.Columns[0], fc.Columns[0]); 48 49 // Columns count 50 Assert.Equal(1, fc.Columns.Length); 51 } 52 53 [Fact] Equals()54 public void Equals() 55 { 56 var ds = new DataSet(); 57 DataTable dtParent = DataProvider.CreateParentDataTable(); 58 DataTable dtChild = DataProvider.CreateChildDataTable(); 59 ds.Tables.Add(dtParent); 60 ds.Tables.Add(dtChild); 61 dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns[0] }; 62 ds.EnforceConstraints = true; 63 64 ForeignKeyConstraint fc1, fc2; 65 fc1 = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 66 67 fc2 = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[1]); 68 // different columnn 69 Assert.Equal(false, fc1.Equals(fc2)); 70 71 //Two System.Data.ForeignKeyConstraint are equal if they constrain the same columns. 72 // same column 73 fc2 = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 74 Assert.Equal(true, fc1.Equals(fc2)); 75 } 76 77 [Fact] RelatedColumns()78 public void RelatedColumns() 79 { 80 var ds = new DataSet(); 81 DataTable dtParent = DataProvider.CreateParentDataTable(); 82 DataTable dtChild = DataProvider.CreateChildDataTable(); 83 ds.Tables.Add(dtParent); 84 ds.Tables.Add(dtChild); 85 ForeignKeyConstraint fc = null; 86 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 87 88 // RelatedColumns 89 Assert.Equal(new DataColumn[] { dtParent.Columns[0] }, fc.RelatedColumns); 90 } 91 92 [Fact] RelatedTable()93 public void RelatedTable() 94 { 95 var ds = new DataSet(); 96 DataTable dtParent = DataProvider.CreateParentDataTable(); 97 DataTable dtChild = DataProvider.CreateChildDataTable(); 98 ds.Tables.Add(dtParent); 99 ds.Tables.Add(dtChild); 100 ForeignKeyConstraint fc = null; 101 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 102 103 // RelatedTable 104 Assert.Equal(dtParent, fc.RelatedTable); 105 } 106 107 [Fact] Table()108 public void Table() 109 { 110 var ds = new DataSet(); 111 DataTable dtParent = DataProvider.CreateParentDataTable(); 112 DataTable dtChild = DataProvider.CreateChildDataTable(); 113 ds.Tables.Add(dtParent); 114 ds.Tables.Add(dtChild); 115 ForeignKeyConstraint fc = null; 116 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 117 118 // Table 119 Assert.Equal(dtChild, fc.Table); 120 } 121 122 [Fact] ToString()123 public new void ToString() 124 { 125 DataTable dtParent = DataProvider.CreateParentDataTable(); 126 DataTable dtChild = DataProvider.CreateChildDataTable(); 127 128 ForeignKeyConstraint fc = null; 129 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 130 131 // ToString - default 132 Assert.Equal(string.Empty, fc.ToString()); 133 134 fc = new ForeignKeyConstraint("myConstraint", dtParent.Columns[0], dtChild.Columns[0]); 135 // Tostring - Constraint name 136 Assert.Equal("myConstraint", fc.ToString()); 137 } 138 139 [Fact] acceptRejectRule()140 public void acceptRejectRule() 141 { 142 DataSet ds = getNewDataSet(); 143 144 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 145 fc.AcceptRejectRule = AcceptRejectRule.Cascade; 146 ds.Tables[1].Constraints.Add(fc); 147 148 //Update the parent 149 150 ds.Tables[0].Rows[0]["ParentId"] = 777; 151 Assert.Equal(true, ds.Tables[1].Select("ParentId=777").Length > 0); 152 ds.Tables[0].RejectChanges(); 153 Assert.Equal(0, ds.Tables[1].Select("ParentId=777").Length); 154 } getNewDataSet()155 private DataSet getNewDataSet() 156 { 157 DataSet ds1 = new DataSet(); 158 ds1.Tables.Add(DataProvider.CreateParentDataTable()); 159 ds1.Tables.Add(DataProvider.CreateChildDataTable()); 160 // ds1.Tables.Add(DataProvider.CreateChildDataTable()); 161 ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] }; 162 163 return ds1; 164 } 165 166 [Fact] constraintName()167 public void constraintName() 168 { 169 var ds = new DataSet(); 170 DataTable dtParent = DataProvider.CreateParentDataTable(); 171 DataTable dtChild = DataProvider.CreateChildDataTable(); 172 ds.Tables.Add(dtParent); 173 ds.Tables.Add(dtChild); 174 175 ForeignKeyConstraint fc = null; 176 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 177 178 // default 179 Assert.Equal(string.Empty, fc.ConstraintName); 180 181 fc.ConstraintName = "myConstraint"; 182 183 // set/get 184 Assert.Equal("myConstraint", fc.ConstraintName); 185 } 186 187 [Fact] ctor_ParentColChildCol()188 public void ctor_ParentColChildCol() 189 { 190 DataTable dtParent = DataProvider.CreateParentDataTable(); 191 DataTable dtChild = DataProvider.CreateChildDataTable(); 192 var ds = new DataSet(); 193 ds.Tables.Add(dtChild); 194 ds.Tables.Add(dtParent); 195 196 ForeignKeyConstraint fc = null; 197 198 // Ctor ArgumentException 199 AssertExtensions.Throws<ArgumentException>(null, () => 200 { 201 fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0], dtChild.Columns[1] }); 202 }); 203 204 fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0], dtParent.Columns[1] }, new DataColumn[] { dtChild.Columns[0], dtChild.Columns[2] }); 205 206 // Add constraint to table - ArgumentException 207 AssertExtensions.Throws<ArgumentException>(null, () => 208 { 209 dtChild.Constraints.Add(fc); 210 }); 211 212 // Child Table Constraints Count - two columnns 213 Assert.Equal(0, dtChild.Constraints.Count); 214 215 // Parent Table Constraints Count - two columnns 216 Assert.Equal(1, dtParent.Constraints.Count); 217 218 // DataSet relations Count 219 Assert.Equal(0, ds.Relations.Count); 220 221 dtParent.Constraints.Clear(); 222 dtChild.Constraints.Clear(); 223 224 fc = new ForeignKeyConstraint(new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0] }); 225 // Ctor 226 Assert.Equal(false, fc == null); 227 228 // Child Table Constraints Count 229 Assert.Equal(0, dtChild.Constraints.Count); 230 231 // Parent Table Constraints Count 232 Assert.Equal(0, dtParent.Constraints.Count); 233 234 // DataSet relations Count 235 Assert.Equal(0, ds.Relations.Count); 236 237 dtChild.Constraints.Add(fc); 238 239 // Child Table Constraints Count, Add 240 Assert.Equal(1, dtChild.Constraints.Count); 241 242 // Parent Table Constraints Count, Add 243 Assert.Equal(1, dtParent.Constraints.Count); 244 245 // DataSet relations Count, Add 246 Assert.Equal(0, ds.Relations.Count); 247 248 // Parent Table Constraints type 249 Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType()); 250 251 // Parent Table Constraints type 252 Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType()); 253 254 // Parent Table Primary key 255 Assert.Equal(0, dtParent.PrimaryKey.Length); 256 257 dtChild.Constraints.Clear(); 258 dtParent.Constraints.Clear(); 259 ds.Relations.Add(new DataRelation("myRelation", dtParent.Columns[0], dtChild.Columns[0])); 260 261 // Relation - Child Table Constraints Count 262 Assert.Equal(1, dtChild.Constraints.Count); 263 264 // Relation - Parent Table Constraints Count 265 Assert.Equal(1, dtParent.Constraints.Count); 266 267 // Relation - Parent Table Constraints type 268 Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType()); 269 270 // Relation - Parent Table Constraints type 271 Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType()); 272 273 // Relation - Parent Table Primary key 274 Assert.Equal(0, dtParent.PrimaryKey.Length); 275 } 276 277 [Fact] ctor_NameParentColChildCol()278 public void ctor_NameParentColChildCol() 279 { 280 DataTable dtParent = DataProvider.CreateParentDataTable(); 281 DataTable dtChild = DataProvider.CreateChildDataTable(); 282 283 ForeignKeyConstraint fc = null; 284 fc = new ForeignKeyConstraint("myForeignKey", dtParent.Columns[0], dtChild.Columns[0]); 285 286 // Ctor 287 Assert.Equal(false, fc == null); 288 289 // Ctor - name 290 Assert.Equal("myForeignKey", fc.ConstraintName); 291 } 292 293 [Fact] ctor_NameParentColsChildCols()294 public void ctor_NameParentColsChildCols() 295 { 296 DataTable dtParent = DataProvider.CreateParentDataTable(); 297 DataTable dtChild = DataProvider.CreateChildDataTable(); 298 299 ForeignKeyConstraint fc = null; 300 fc = new ForeignKeyConstraint("myForeignKey", new DataColumn[] { dtParent.Columns[0] }, new DataColumn[] { dtChild.Columns[0] }); 301 302 // Ctor 303 Assert.Equal(false, fc == null); 304 305 // Ctor - name 306 Assert.Equal("myForeignKey", fc.ConstraintName); 307 } 308 309 [Fact] deleteRule()310 public void deleteRule() 311 { 312 var ds = new DataSet(); 313 DataTable dtParent = DataProvider.CreateParentDataTable(); 314 DataTable dtChild = DataProvider.CreateChildDataTable(); 315 ds.Tables.Add(dtParent); 316 ds.Tables.Add(dtChild); 317 dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns[0] }; 318 ds.EnforceConstraints = true; 319 320 ForeignKeyConstraint fc = null; 321 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 322 323 //checking default 324 // Default 325 Assert.Equal(Rule.Cascade, fc.DeleteRule); 326 327 //checking set/get 328 foreach (Rule rule in Enum.GetValues(typeof(Rule))) 329 { 330 // Set/Get - rule 331 fc.DeleteRule = rule; 332 Assert.Equal(rule, fc.DeleteRule); 333 } 334 335 dtChild.Constraints.Add(fc); 336 337 //checking delete rule 338 339 // Rule = None, Delete Exception 340 fc.DeleteRule = Rule.None; 341 //Exception = "Cannot delete this row because constraints are enforced on relation Constraint1, and deleting this row will strand child rows." 342 Assert.Throws<InvalidConstraintException>(() => 343 { 344 dtParent.Rows.Find(1).Delete(); 345 }); 346 347 // Rule = None, Delete succeed 348 fc.DeleteRule = Rule.None; 349 foreach (DataRow dr in dtChild.Select("ParentId = 1")) 350 dr.Delete(); 351 dtParent.Rows.Find(1).Delete(); 352 Assert.Equal(0, dtParent.Select("ParentId=1").Length); 353 354 // Rule = Cascade 355 fc.DeleteRule = Rule.Cascade; 356 dtParent.Rows.Find(2).Delete(); 357 Assert.Equal(0, dtChild.Select("ParentId=2").Length); 358 359 // Rule = SetNull 360 DataSet ds1 = new DataSet(); 361 ds1.Tables.Add(DataProvider.CreateParentDataTable()); 362 ds1.Tables.Add(DataProvider.CreateChildDataTable()); 363 364 ForeignKeyConstraint fc1 = new ForeignKeyConstraint(ds1.Tables[0].Columns[0], ds1.Tables[1].Columns[1]); 365 fc1.DeleteRule = Rule.SetNull; 366 ds1.Tables[1].Constraints.Add(fc1); 367 368 Assert.Equal(0, ds1.Tables[1].Select("ChildId is null").Length); 369 370 ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] }; 371 ds1.Tables[0].Rows.Find(3).Delete(); 372 373 ds1.Tables[0].AcceptChanges(); 374 ds1.Tables[1].AcceptChanges(); 375 376 DataRow[] arr = ds1.Tables[1].Select("ChildId is null"); 377 378 /*foreach (DataRow dr in arr) 379 { 380 Assert.Equal(null, dr["ChildId"]); 381 }*/ 382 383 Assert.Equal(4, arr.Length); 384 385 // Rule = SetDefault 386 //fc.DeleteRule = Rule.SetDefault; 387 ds1 = new DataSet(); 388 ds1.Tables.Add(DataProvider.CreateParentDataTable()); 389 ds1.Tables.Add(DataProvider.CreateChildDataTable()); 390 391 fc1 = new ForeignKeyConstraint(ds1.Tables[0].Columns[0], ds1.Tables[1].Columns[1]); 392 fc1.DeleteRule = Rule.SetDefault; 393 ds1.Tables[1].Constraints.Add(fc1); 394 ds1.Tables[1].Columns[1].DefaultValue = "777"; 395 396 //Add new row --> in order to apply the forigen key rules 397 DataRow dr2 = ds1.Tables[0].NewRow(); 398 dr2["ParentId"] = 777; 399 ds1.Tables[0].Rows.Add(dr2); 400 401 ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] }; 402 ds1.Tables[0].Rows.Find(3).Delete(); 403 Assert.Equal(4, ds1.Tables[1].Select("ChildId=777").Length); 404 } 405 406 [Fact] extendedProperties()407 public void extendedProperties() 408 { 409 DataTable dtParent = DataProvider.CreateParentDataTable(); 410 DataTable dtChild = DataProvider.CreateParentDataTable(); 411 412 ForeignKeyConstraint fc = null; 413 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 414 415 PropertyCollection pc = fc.ExtendedProperties; 416 417 // Checking ExtendedProperties default 418 Assert.Equal(true, fc != null); 419 420 // Checking ExtendedProperties count 421 Assert.Equal(0, pc.Count); 422 } 423 424 [Fact] ctor_DclmDclm()425 public void ctor_DclmDclm() 426 { 427 DataTable dtParent = DataProvider.CreateParentDataTable(); 428 DataTable dtChild = DataProvider.CreateChildDataTable(); 429 var ds = new DataSet(); 430 ds.Tables.Add(dtChild); 431 ds.Tables.Add(dtParent); 432 433 ForeignKeyConstraint fc = null; 434 fc = new ForeignKeyConstraint(dtParent.Columns[0], dtChild.Columns[0]); 435 436 Assert.False(fc == null); 437 438 Assert.Equal(0, dtChild.Constraints.Count); 439 440 Assert.Equal(0, dtParent.Constraints.Count); 441 442 Assert.Equal(0, ds.Relations.Count); 443 444 dtChild.Constraints.Add(fc); 445 446 Assert.Equal(1, dtChild.Constraints.Count); 447 448 Assert.Equal(1, dtParent.Constraints.Count); 449 450 Assert.Equal(0, ds.Relations.Count); 451 452 Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType()); 453 454 Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType()); 455 456 Assert.Equal(0, dtParent.PrimaryKey.Length); 457 458 dtChild.Constraints.Clear(); 459 dtParent.Constraints.Clear(); 460 ds.Relations.Add(new DataRelation("myRelation", dtParent.Columns[0], dtChild.Columns[0])); 461 462 Assert.Equal(1, dtChild.Constraints.Count); 463 464 Assert.Equal(1, dtParent.Constraints.Count); 465 466 Assert.Equal(typeof(UniqueConstraint), dtParent.Constraints[0].GetType()); 467 468 Assert.Equal(typeof(ForeignKeyConstraint), dtChild.Constraints[0].GetType()); 469 470 Assert.Equal(0, dtParent.PrimaryKey.Length); 471 } 472 473 [Fact] ctor_DclmDclm1()474 public void ctor_DclmDclm1() 475 { 476 Assert.Throws<NullReferenceException>(() => 477 { 478 ForeignKeyConstraint fc = new ForeignKeyConstraint(null, (DataColumn)null); 479 }); 480 } 481 482 [Fact] ctor_DclmDclm2()483 public void ctor_DclmDclm2() 484 { 485 AssertExtensions.Throws<ArgumentException>(null, () => 486 { 487 var ds = new DataSet(); 488 ds.Tables.Add(DataProvider.CreateParentDataTable()); 489 ds.Tables.Add(DataProvider.CreateChildDataTable()); 490 ds.Tables["Parent"].Columns["ParentId"].Expression = "2"; 491 492 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 493 }); 494 } 495 496 [Fact] ctor_DclmDclm3()497 public void ctor_DclmDclm3() 498 { 499 AssertExtensions.Throws<ArgumentException>(null, () => 500 { 501 var ds = new DataSet(); 502 ds.Tables.Add(DataProvider.CreateParentDataTable()); 503 ds.Tables.Add(DataProvider.CreateChildDataTable()); 504 ds.Tables["Child"].Columns["ParentId"].Expression = "2"; 505 506 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 507 }); 508 } 509 510 [Fact] UpdateRule1()511 public void UpdateRule1() 512 { 513 DataSet ds = GetNewDataSet(); 514 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 515 fc.UpdateRule = Rule.Cascade; 516 ds.Tables[1].Constraints.Add(fc); 517 518 //Changing parent row 519 520 ds.Tables[0].Rows.Find(1)["ParentId"] = 8; 521 522 ds.Tables[0].AcceptChanges(); 523 ds.Tables[1].AcceptChanges(); 524 //Checking the table 525 526 Assert.True(ds.Tables[1].Select("ParentId=8").Length > 0); 527 } 528 529 [Fact] UpdateRule2()530 public void UpdateRule2() 531 { 532 Assert.Throws<ConstraintException>(() => 533 { 534 DataSet ds = GetNewDataSet(); 535 ds.Tables[0].PrimaryKey = null; 536 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 537 fc.UpdateRule = Rule.None; 538 ds.Tables[1].Constraints.Add(fc); 539 540 //Changing parent row 541 542 ds.Tables[0].Rows[0]["ParentId"] = 5; 543 544 /*ds.Tables[0].AcceptChanges(); 545 ds.Tables[1].AcceptChanges(); 546 //Checking the table 547 Compare(ds.Tables[1].Select("ParentId=8").Length ,0);*/ 548 }); 549 } 550 551 [Fact] UpdateRule3()552 public void UpdateRule3() 553 { 554 DataSet ds = GetNewDataSet(); 555 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[1]); 556 fc.UpdateRule = Rule.SetDefault; 557 ds.Tables[1].Constraints.Add(fc); 558 559 //Changing parent row 560 561 ds.Tables[1].Columns[1].DefaultValue = "777"; 562 563 //Add new row --> in order to apply the forigen key rules 564 DataRow dr = ds.Tables[0].NewRow(); 565 dr["ParentId"] = 777; 566 ds.Tables[0].Rows.Add(dr); 567 568 569 ds.Tables[0].Rows.Find(1)["ParentId"] = 8; 570 571 ds.Tables[0].AcceptChanges(); 572 ds.Tables[1].AcceptChanges(); 573 //Checking the table 574 575 Assert.True(ds.Tables[1].Select("ChildId=777").Length > 0); 576 } 577 578 [Fact] UpdateRule4()579 public void UpdateRule4() 580 { 581 DataSet ds = GetNewDataSet(); 582 ForeignKeyConstraint fc = new ForeignKeyConstraint(ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]); 583 fc.UpdateRule = Rule.SetNull; 584 ds.Tables[1].Constraints.Add(fc); 585 586 //Changing parent row 587 588 ds.Tables[0].Rows.Find(1)["ParentId"] = 8; 589 590 ds.Tables[0].AcceptChanges(); 591 ds.Tables[1].AcceptChanges(); 592 //Checking the table 593 594 Assert.True(ds.Tables[1].Select("ParentId is null").Length > 0); 595 } 596 GetNewDataSet()597 private DataSet GetNewDataSet() 598 { 599 DataSet ds1 = new DataSet(); 600 ds1.Tables.Add(DataProvider.CreateParentDataTable()); 601 ds1.Tables.Add(DataProvider.CreateChildDataTable()); 602 ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns[0] }; 603 604 return ds1; 605 } 606 [Fact] ForeignConstraint_DateTimeModeTest()607 public void ForeignConstraint_DateTimeModeTest() 608 { 609 DataTable t1 = new DataTable("t1"); 610 t1.Columns.Add("col", typeof(DateTime)); 611 612 DataTable t2 = new DataTable("t2"); 613 t2.Columns.Add("col", typeof(DateTime)); 614 t2.Columns[0].DateTimeMode = DataSetDateTime.Unspecified; 615 616 // DataColumn type shud match, and no exception shud be raised 617 t2.Constraints.Add("fk", t1.Columns[0], t2.Columns[0]); 618 619 t2.Constraints.Clear(); 620 t2.Columns[0].DateTimeMode = DataSetDateTime.Local; 621 try 622 { 623 // DataColumn type shud not match, and exception shud be raised 624 t2.Constraints.Add("fk", t1.Columns[0], t2.Columns[0]); 625 Assert.False(true); 626 } 627 catch (InvalidOperationException e) { } 628 } 629 630 [Fact] ParentChildSameColumn()631 public void ParentChildSameColumn() 632 { 633 DataTable dataTable = new DataTable("Menu"); 634 DataColumn colID = dataTable.Columns.Add("ID", typeof(int)); 635 DataColumn colCulture = dataTable.Columns.Add("Culture", typeof(string)); 636 dataTable.Columns.Add("Name", typeof(string)); 637 DataColumn colParentID = dataTable.Columns.Add("ParentID", typeof(int)); 638 639 // table PK (ID, Culture) 640 dataTable.Constraints.Add(new UniqueConstraint( 641 "MenuPK", 642 new DataColumn[] { colID, colCulture }, 643 true)); 644 645 // add a FK referencing the same table: (ID, Culture) <- (ParentID, Culture) 646 ForeignKeyConstraint fkc = new ForeignKeyConstraint( 647 "MenuParentFK", 648 new DataColumn[] { colID, colCulture }, 649 new DataColumn[] { colParentID, colCulture }); 650 651 dataTable.Constraints.Add(fkc); 652 } 653 } 654 } 655