public virtual void Can_configure_one_to_one_relationship_from_an_owned_type(TestModelBuilder modelBuilder) { var model = modelBuilder.Model; modelBuilder.Ignore<Customer>(); modelBuilder.Entity<OtherCustomer>().OwnsOne(c => c.Details) .HasOne<SpecialCustomer>() .WithOne() .HasPrincipalKey<SpecialCustomer>(); Assert.NotNull(model.FindEntityType(typeof(CustomerDetails))); modelBuilder.Entity<SpecialCustomer>().OwnsOne(c => c.Details); modelBuilder.Validate(); var ownership = model.FindEntityType(typeof(OtherCustomer)).FindNavigation(nameof(Customer.Details)).ForeignKey; var foreignKey = model.FindEntityType(typeof(SpecialCustomer)).GetReferencingForeignKeys() .Single(fk => fk.DeclaringEntityType.ClrType == typeof(CustomerDetails) && fk.PrincipalToDependent == null); Assert.Same(ownership.DeclaringEntityType, foreignKey.DeclaringEntityType); Assert.NotEqual(ownership.Properties.Single().Name, foreignKey.Properties.Single().Name); Assert.Equal(2, model.GetEntityTypes().Count(e => e.ClrType == typeof(CustomerDetails))); Assert.Equal(2, ownership.DeclaringEntityType.GetForeignKeys().Count()); }
public virtual void Weak_types_with_FK_to_another_entity_works(TestModelBuilder modelBuilder) { var ownerEntityTypeBuilder = modelBuilder.Entity <BillingOwner>(); ownerEntityTypeBuilder.OwnsOne(e => e.Bill1, o => o.HasOne <Country>().WithMany().HasPrincipalKey(c => c.Name).HasForeignKey(d => d.Country)); ownerEntityTypeBuilder.OwnsOne(e => e.Bill2, o => o.HasOne <Country>().WithMany().HasPrincipalKey(c => c.Name).HasForeignKey(d => d.Country)); modelBuilder.Validate(); }
public void TPC_with_IA_on_non_leaf_type_not_allowed() { new Exception("The association 'Related_to_Base' between entity types 'Related' and 'Base' is invalid. In a TPC hierarchy independent associations are only allowed on the most derived types.") .ValidateMessage("EntityMappingConfiguration_TPCWithIAsOnNonLeafType", "Related_to_Base", "Related", "Base"); var modelBuilder = new TestModelBuilder() .Entity("Base") .Key("P1") .Property("P2") .Subclass("Derived"); modelBuilder.Entity("Related"); modelBuilder.Association( "Related_To_Base", "Related", RelationshipMultiplicity.One, "BaseEntities", "Base", RelationshipMultiplicity.Many, "RelatedEntities"); EdmModel model = modelBuilder; var databaseMapping = new DatabaseMappingGenerator(ProviderRegistry.Sql2008_ProviderInfo, ProviderRegistry.Sql2008_ProviderManifest) .Generate(model); var modelConfiguration = new ModelConfiguration(); modelConfiguration.Entity(model.GetEntityType("Derived").GetClrType()) .AddMappingConfiguration( new EntityMappingConfiguration { MapInheritedProperties = true, TableName = new DatabaseName("DerivedEntities") }); Assert.Throws<InvalidOperationException>( () => modelConfiguration.Configure(databaseMapping, ProviderRegistry.Sql2008_ProviderManifest)) .ValidateMessage("EntityMappingConfiguration_TPCWithIAsOnNonLeafType", "Related_To_Base", "Related", "Base"); }