1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace FunctionalTests
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Linq.Expressions;
using FunctionalTests.Model;
using Xunit;
public class DataAnnotationScenarioTests : TestBase
{
[Fact]
public void Duplicate_column_order_should_not_throw_when_v1_convention_set()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Entity_10558>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
}
[Fact]
public void Duplicate_column_order_should_throw_when_v2_convention_set()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V5_0);
modelBuilder.Entity<Entity_10558>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("DuplicateConfiguredColumnOrder", "Entity_10558");
}
[Fact]
public void Non_public_annotations_are_enabled()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<PrivateMemberAnnotationClass>().Property(
PrivateMemberAnnotationClass.PersonFirstNameExpr);
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert(PrivateMemberAnnotationClass.PersonFirstNameObjectExpr)
.DbEqual("dsdsd", c => c.Name)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(128, f => f.MaxLength)
.DbEqual(true, c => c.IsPrimaryKeyColumn);
}
[Fact]
public void NotMapped_should_propagate_down_inheritance_hierachy()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<NotMappedDerived>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("InvalidEntityType", typeof(NotMappedDerived));
}
[Fact]
public void NotMapped_on_base_class_property_ignores_it()
{
using (var baseEntityConfiguration = new DynamicTypeDescriptionConfiguration<BaseEntity>())
{
var modelBuilder = new DbModelBuilder();
baseEntityConfiguration.SetPropertyAttributes(b => b.BaseClassProperty, new NotMappedAttribute());
baseEntityConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Entity<Unit>();
modelBuilder.Entity<BaseEntity>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.SelectMany(e => e.Properties)
.Any(p => p.Name == "BaseClassProperty"));
Assert.False(
databaseMapping.Model.EntityTypes.SelectMany(e => e.Properties)
.Any(p => p.Name == "VirtualBaseClassProperty"));
}
}
[Fact]
public void NotMapped_on_base_class_property_and_overriden_property_ignores_them()
{
using (var baseEntityConfiguration = new DynamicTypeDescriptionConfiguration<BaseEntity>())
{
using (var unitConfiguration = new DynamicTypeDescriptionConfiguration<Unit>())
{
var modelBuilder = new DbModelBuilder();
unitConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
baseEntityConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Entity<Unit>();
modelBuilder.Entity<BaseEntity>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.SelectMany(e => e.Properties)
.Any(p => p.Name == "VirtualBaseClassProperty"));
}
}
}
[Fact]
public void NotMapped_on_base_class_property_discovered_through_navigation_ignores_it()
{
using (var abstractBaseEntityConfiguration = new DynamicTypeDescriptionConfiguration<AbstractBaseEntity>())
{
var modelBuilder = new DbModelBuilder();
abstractBaseEntityConfiguration.SetPropertyAttributes(b => b.AbstractBaseClassProperty, new NotMappedAttribute());
modelBuilder.Entity<Unit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.True(databaseMapping.Model.EntityTypes.Any(e => e.Name == "AbstractBaseEntity"));
Assert.False(
databaseMapping.Model.EntityTypes.SelectMany(e => e.Properties)
.Any(p => p.Name == "AbstractBaseClassProperty"));
}
}
[Fact]
public void NotMapped_on_abstract_base_class_property_ignores_it()
{
using (var abstractBaseEntityConfiguration = new DynamicTypeDescriptionConfiguration<AbstractBaseEntity>())
{
var modelBuilder = new DbModelBuilder();
abstractBaseEntityConfiguration.SetPropertyAttributes(b => b.AbstractBaseClassProperty, new NotMappedAttribute());
modelBuilder.Entity<AbstractBaseEntity>();
modelBuilder.Entity<BaseEntity>();
modelBuilder.Entity<Unit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.SelectMany(e => e.Properties)
.Any(p => p.Name == "AbstractBaseClassProperty"));
}
}
[Fact]
public void NotMapped_on_overriden_mapped_base_class_property_throws()
{
using (var unitConfiguration = new DynamicTypeDescriptionConfiguration<Unit>())
{
var modelBuilder = new DbModelBuilder();
unitConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Ignore<DifferentUnit>();
modelBuilder.Entity<Unit>();
modelBuilder.Entity<BaseEntity>();
Assert.Throws<InvalidOperationException>(() => BuildMapping(modelBuilder))
.ValidateMessage(
"CannotIgnoreMappedBaseProperty",
"VirtualBaseClassProperty", "FunctionalTests.Unit",
"FunctionalTests.BaseEntity");
}
}
[Fact]
public void NotMapped_on_unmapped_derived_property_ignores_it()
{
using (var unitConfiguration = new DynamicTypeDescriptionConfiguration<Unit>())
{
var modelBuilder = new DbModelBuilder();
unitConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Ignore<AbstractBaseEntity>();
modelBuilder.Ignore<BaseEntity>();
modelBuilder.Entity<Unit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.Single().Properties.Any(
p => p.Name == "VirtualBaseClassProperty"));
}
}
[Fact]
public void NotMapped_on_unmapped_base_class_property_and_overriden_property_ignores_it()
{
using (var unitConfiguration = new DynamicTypeDescriptionConfiguration<Unit>())
{
using (var baseEntityConfiguration = new DynamicTypeDescriptionConfiguration<BaseEntity>())
{
var modelBuilder = new DbModelBuilder();
baseEntityConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
unitConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Ignore<AbstractBaseEntity>();
modelBuilder.Ignore<BaseEntity>();
modelBuilder.Entity<Unit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.Single().Properties.Any(
p => p.Name == "VirtualBaseClassProperty"));
}
}
}
[Fact]
public void NotMapped_on_unmapped_base_class_property_ignores_it()
{
using (var baseEntityConfiguration = new DynamicTypeDescriptionConfiguration<BaseEntity>())
{
var modelBuilder = new DbModelBuilder();
baseEntityConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Ignore<AbstractBaseEntity>();
modelBuilder.Ignore<BaseEntity>();
modelBuilder.Entity<Unit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.Single().Properties.Any(
p => p.Name == "VirtualBaseClassProperty"));
}
}
[Fact]
public void NotMapped_on_new_property_with_same_name_as_in_unmapped_base_class_ignores_it()
{
using (var differentUnitConfiguration = new DynamicTypeDescriptionConfiguration<DifferentUnit>())
{
var modelBuilder = new DbModelBuilder();
differentUnitConfiguration.SetPropertyAttributes(b => b.VirtualBaseClassProperty, new NotMappedAttribute());
modelBuilder.Entity<DifferentUnit>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.False(
databaseMapping.Model.EntityTypes.Single().Properties.Any(
p => p.Name == "VirtualBaseClassProperty"));
}
}
[Fact]
public void MaxLength_takes_presedence_over_StringLength()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthAnnotationClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthAnnotationClass>(x => x.PersonFirstName).FacetEqual(true, a => a.IsMaxLength);
}
[Fact]
public void MaxLength_with_length_takes_precedence_over_StringLength()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthWithLengthAnnotationClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthWithLengthAnnotationClass>(x => x.PersonFirstName).FacetEqual(
30,
a =>
a.MaxLength);
}
[Fact]
public void Default_length_for_key_string_column()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
loginConfiguration.SetPropertyAttributes(l => l.UserName, new KeyAttribute());
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Ignore<Profile>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Login>(x => x.UserName)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(128, f => f.MaxLength);
}
}
[Fact]
public void Key_and_column_work_together()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<ColumnKeyAnnotationClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ColumnKeyAnnotationClass>(x => x.PersonFirstName)
.DbEqual("dsdsd", c => c.Name)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(128, f => f.MaxLength)
.DbEqual(true, c => c.IsPrimaryKeyColumn);
}
[Fact]
// Regression test for Dev11 Bug 87347
public void Key_and_column_work_together_in_an_IA()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<ColumnKeyAnnotationClass>();
modelBuilder.Entity<ReferencingClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ColumnKeyAnnotationClass>(x => x.PersonFirstName)
.DbEqual("dsdsd", c => c.Name)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(128, f => f.MaxLength)
.DbEqual(true, c => c.IsPrimaryKeyColumn);
Assert.Equal(1, databaseMapping.Model.AssociationTypes.Count());
databaseMapping.Assert<ReferencingClass>().ForeignKeyColumn("Person_PersonFirstName")
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(128, f => f.MaxLength);
}
[Fact]
public void Key_column_and_MaxLength_work_together()
{
using (var entityClassConfiguration = new DynamicTypeDescriptionConfiguration<ColumnKeyAnnotationClass>())
{
entityClassConfiguration.SetPropertyAttributes(c => c.PersonFirstName, new MaxLengthAttribute(64));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<ColumnKeyAnnotationClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ColumnKeyAnnotationClass>(x => x.PersonFirstName)
.DbEqual("dsdsd", c => c.Name)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(64, f => f.MaxLength)
.DbEqual(true, c => c.IsPrimaryKeyColumn);
}
}
[Fact]
public void Key_column_and_MaxLength_work_together_in_an_IA()
{
using (var entityClassConfiguration = new DynamicTypeDescriptionConfiguration<ColumnKeyAnnotationClass>())
{
entityClassConfiguration.SetPropertyAttributes(c => c.PersonFirstName, new MaxLengthAttribute(64));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<ColumnKeyAnnotationClass>();
modelBuilder.Entity<ReferencingClass>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ColumnKeyAnnotationClass>(x => x.PersonFirstName)
.DbEqual("dsdsd", c => c.Name)
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(64, f => f.MaxLength)
.DbEqual(true, c => c.IsPrimaryKeyColumn);
Assert.Equal(1, databaseMapping.Model.AssociationTypes.Count());
databaseMapping.Assert<ReferencingClass>().ForeignKeyColumn("Person_PersonFirstName")
.DbEqual("nvarchar", c => c.TypeName)
.DbEqual(64, f => f.MaxLength);
}
}
[Fact]
public void Key_from_base_type_is_recognized()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<SRelated>();
modelBuilder.Entity<OKeyBase>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
// one thing configured because of key property
Assert.Equal(1, modelBuilder.ModelConfiguration.Entity(typeof(OKeyBase)).ConfiguredProperties.Count());
// should be nothing configured on derived type
Assert.Equal(0, modelBuilder.ModelConfiguration.Entity(typeof(DODerived)).ConfiguredProperties.Count());
}
[Fact]
public void Key_on_nav_prop_is_ignored()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<KeyOnNavProp>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<KeyOnNavProp>().DbEqual("Id", t => t.DeclaredKeyProperties.Single().Name);
}
[Fact]
public void Timestamp_takes_precedence_over_MaxLength()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<TimestampAndMaxlen>().Ignore(x => x.NonMaxTimestamp);
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
"rowversion",
t =>
t.Properties.Single(x => x.Name == "MaxTimestamp").
TypeName);
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
false,
t =>
t.Properties.Single(x => x.Name == "MaxTimestamp").IsMaxLength);
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
null,
t =>
t.Properties.Single(x => x.Name == "MaxTimestamp").MaxLength);
}
[Fact]
public void Timestamp_takes_precedence_over_MaxLength_with_value()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<TimestampAndMaxlen>().Ignore(x => x.MaxTimestamp);
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
"rowversion",
t =>
t.Properties.Single(x => x.Name == "NonMaxTimestamp").
TypeName);
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
false,
t =>
t.Properties.Single(x => x.Name == "NonMaxTimestamp").IsMaxLength);
databaseMapping.Assert<TimestampAndMaxlen>().DbEqual(
null,
t =>
t.Properties.Single(x => x.Name == "NonMaxTimestamp").MaxLength);
}
[Fact]
public void Annotation_in_derived_class_when_base_class_processed_after_derived_class()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<StyledProduct>();
modelBuilder.Entity<Product>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.Assert<StyledProduct>(s => s.Style).FacetEqual(150, f => f.MaxLength);
}
[Fact]
public void Required_and_ForeignKey_to_Required()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(
l => l.Profile, new RequiredAttribute(),
new ForeignKeyAttribute("LoginId"));
profileConfiguration.SetPropertyAttributes(p => p.User, new RequiredAttribute());
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Login), typeof(Profile));
}
}
}
[Fact]
// Regression test for Dev11 Bug 94993
public void Required_to_Required_and_ForeignKey()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile, new RequiredAttribute());
profileConfiguration.SetPropertyAttributes(
p => p.User, new RequiredAttribute(),
new ForeignKeyAttribute("ProfileId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Login), typeof(Profile));
}
}
}
[Fact]
public void Required_and_ForeignKey_to_Required_and_ForeignKey()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(
l => l.Profile, new RequiredAttribute(),
new ForeignKeyAttribute("LoginId"));
profileConfiguration.SetPropertyAttributes(
p => p.User, new RequiredAttribute(),
new ForeignKeyAttribute("ProfileId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Login), typeof(Profile));
}
}
}
[Fact]
public void ForeignKey_to_nothing()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile, new ForeignKeyAttribute("LoginId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Profile), typeof(Login));
}
}
}
[Fact]
public void Required_and_ForeignKey_to_nothing()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile, new ForeignKeyAttribute("LoginId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Profile), typeof(Login));
}
}
}
[Fact]
public void Nothing_to_ForeignKey()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile);
profileConfiguration.SetPropertyAttributes(p => p.User, new ForeignKeyAttribute("ProfileId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Profile), typeof(Login));
}
}
}
[Fact]
public void Nothing_to_Required_and_ForeignKey()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile);
profileConfiguration.SetPropertyAttributes(
p => p.User, new RequiredAttribute(),
new ForeignKeyAttribute("ProfileId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
var association = databaseMapping.Model.AssociationTypes.Single();
Assert.Equal("Profile", association.SourceEnd.GetEntityType().Name);
Assert.Equal(RelationshipMultiplicity.ZeroOrOne, association.SourceEnd.RelationshipMultiplicity);
Assert.Equal("Login", association.TargetEnd.GetEntityType().Name);
Assert.Equal(RelationshipMultiplicity.One, association.TargetEnd.RelationshipMultiplicity);
Assert.Equal("Profile", association.Constraint.ToRole.GetEntityType().Name);
}
}
}
[Fact]
public void ForeignKey_to_ForeignKey()
{
using (var loginConfiguration = new DynamicTypeDescriptionConfiguration<Login>())
{
using (var profileConfiguration = new DynamicTypeDescriptionConfiguration<Profile>())
{
loginConfiguration.SetPropertyAttributes(l => l.Profile, new ForeignKeyAttribute("LoginId"));
profileConfiguration.SetPropertyAttributes(p => p.User, new ForeignKeyAttribute("ProfileId"));
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Login>();
modelBuilder.Entity<Profile>();
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder))
.ValidateMessage("UnableToDeterminePrincipal", typeof(Profile), typeof(Login));
}
}
}
[Fact]
public void TableNameAttribute_affects_only_base_in_TPT()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<TNAttrBase>()
.Map<TNAttrDerived>(mc => mc.ToTable("B"));
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.Assert<TNAttrBase>("A");
databaseMapping.Assert<TNAttrDerived>("B");
}
[Fact]
public void TableNameAttribute_affects_table_name_in_TPH()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<TNAttrBase>()
.Map(mc => mc.Requires("disc").HasValue("A"))
.Map<TNAttrDerived>(mc => mc.Requires("disc").HasValue("B"));
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.Assert<TNAttrBase>("A");
databaseMapping.AssertMapping<TNAttrBase>("A", false).HasColumnCondition("disc", "A");
databaseMapping.Assert<TNAttrDerived>("A");
databaseMapping.AssertMapping<TNAttrDerived>("A").HasColumnCondition("disc", "B");
}
}
#region Fixtures
public class MaxLengthAnnotationClass
{
public int Id { get; set; }
[StringLength(500)]
[MaxLength]
public string PersonFirstName { get; set; }
}
public class MaxLengthWithLengthAnnotationClass
{
public int Id { get; set; }
[StringLength(500)]
[MaxLength(30)]
public string PersonFirstName { get; set; }
}
public class ColumnKeyAnnotationClass
{
[Key]
[Column("dsdsd", Order = 1, TypeName = "nvarchar")]
public string PersonFirstName { get; set; }
}
public class ReferencingClass
{
public int Id { get; set; }
public ColumnKeyAnnotationClass Person { get; set; }
}
public class DASimple
{
public int Id { get; set; }
}
public class KeyOnNavProp
{
public int Id { get; set; }
[Key]
public ICollection<DASimple> Simples { get; set; }
[Key]
public DASimple SpecialSimple { get; set; }
}
public class SRelated
{
public int SRelatedId { get; set; }
public ICollection<DODerived> DADeriveds { get; set; }
}
public class OKeyBase
{
[Key]
public int OrderLineNo { get; set; }
public int Quantity { get; set; }
}
public class DODerived : OKeyBase
{
public SRelated DARelated { get; set; }
public string Special { get; set; }
}
public class TimestampAndMaxlen
{
public int Id { get; set; }
[MaxLength]
[Timestamp]
public byte[] MaxTimestamp { get; set; }
[MaxLength(100)]
[Timestamp]
public byte[] NonMaxTimestamp { get; set; }
}
public class Login
{
public int LoginId { get; set; }
public string UserName { get; set; }
public virtual Profile Profile { get; set; }
}
public class Profile
{
public int ProfileId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public virtual Login User { get; set; }
}
[Table("A")]
public class TNAttrBase
{
public int Id { get; set; }
public string BaseData { get; set; }
}
public class TNAttrDerived : TNAttrBase
{
public string DerivedData { get; set; }
}
[NotMapped]
public class NotMappedBase
{
public int Id { get; set; }
}
public class NotMappedDerived : NotMappedBase
{
}
public class PrivateMemberAnnotationClass
{
public static Expression<Func<PrivateMemberAnnotationClass, string>> PersonFirstNameExpr =
p => p.PersonFirstName;
public static Expression<Func<PrivateMemberAnnotationClass, object>> PersonFirstNameObjectExpr =
p => p.PersonFirstName;
[Key]
[Column("dsdsd", Order = 1, TypeName = "nvarchar")]
private string PersonFirstName { get; set; }
}
public class Entity_10558
{
[Key]
[Column(Order = 1)]
public int Key1 { get; set; }
[Key]
[Column(Order = 1)]
public int Key2 { get; set; }
public string Name { get; set; }
}
#endregion
#region Bug324763
namespace Bug324763
{
using System.Data.Entity.ModelConfiguration.Conventions;
public class Product
{
[Timestamp]
public byte[] Version { get; set; }
[Key]
[Column(Order = 0)]
public int ProductId { get; set; }
[Key]
[Column(Order = 1)]
[MaxLength(128)]
public string Sku { get; set; }
[Required]
[StringLength(15)]
public string Name { get; set; }
public byte[] Image { get; set; }
[InverseProperty("Product")]
public ICollection<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int OrderId { get; set; }
[NotMapped]
public short Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
public bool? IsShipped { get; set; }
[ForeignKey("Product")]
[Column(Order = 0)]
public int ProductIdFk { get; set; }
[ForeignKey("Product")]
[Column(Order = 1)]
public string ProductSkuFk { get; set; }
[MaxLength(128)]
public string Sku { get; set; }
[ConcurrencyCheck]
public int EngineSupplierId { get; set; }
public Product Product { get; set; }
}
public class Test324763 : FunctionalTestBase
{
[Fact]
public void Repro324763_Build_Is_Not_Idempotent()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Product>();
modelBuilder.Entity<OrderLine>();
ValidateBuildIsIdempotent(modelBuilder);
}
private void ValidateBuildIsIdempotent(DbModelBuilder modelBuilder)
{
var mapping1 = BuildMapping(modelBuilder);
var mapping2 = BuildMapping(modelBuilder);
Assert.True(mapping1.EdmxIsEqualTo(mapping2));
}
[Fact]
public void Repro324763_Build_Is_Not_Idempotent_Inverse()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Conventions.Remove<AssociationInverseDiscoveryConvention>();
modelBuilder.Entity<Product>();
modelBuilder.Entity<OrderLine>();
ValidateBuildIsIdempotent(modelBuilder);
}
}
}
#endregion
}
|