public void Does_not_throw_when_navigation_is_added()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NavigationEntity), ConfigurationSource.Convention);
            var referencedEntityTypeBuilder = modelBuilder.Entity(typeof(PrimitivePropertyEntity), ConfigurationSource.Convention);
            referencedEntityTypeBuilder.Ignore("Property", ConfigurationSource.Convention);
            entityTypeBuilder.Relationship(referencedEntityTypeBuilder, "Navigation", null, ConfigurationSource.Convention);

            new PropertyMappingValidationConvention().Apply(modelBuilder);
        }
        private InternalIndexBuilder CreateInternalIndexBuilder()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);

            return entityBuilder.HasIndex(new[] { Customer.IdProperty, Customer.NameProperty }, ConfigurationSource.Explicit);
        }
        public void Does_not_throw_when_primitive_type_is_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(PrimitivePropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Ignore("Property", ConfigurationSource.Convention);

            new RelationalPropertyMappingValidationConvention(new TestRelationalTypeMapper()).Apply(modelBuilder);
        }
        public void Does_not_throw_when_primitive_type_is_added()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(PrimitivePropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Property("Property", typeof(int), ConfigurationSource.Convention);

            new PropertyMappingValidationConvention().Apply(modelBuilder);
        }
        public void Throws_when_primitive_type_is_not_added_or_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(PrimitivePropertyEntity), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.PropertyNotAdded("Property", typeof(PrimitivePropertyEntity).FullName),
                Assert.Throws<InvalidOperationException>(() => new RelationalPropertyMappingValidationConvention(new TestRelationalTypeMapper()).Apply(modelBuilder)).Message);
        }
        public void Does_not_throw_when_navigation_is_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NavigationEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Ignore("Navigation", ConfigurationSource.Convention);

            new PropertyMappingValidationConvention().Apply(modelBuilder);
        }
        public void Throws_when_navigation_is_not_added_or_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NavigationEntity), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.NavigationNotAdded("Navigation", typeof(NavigationEntity).FullName),
                Assert.Throws<InvalidOperationException>(() => new PropertyMappingValidationConvention().Apply(modelBuilder)).Message);
        }
        public virtual void Does_not_throw_when_primitive_type_property_is_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(PrimitivePropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Ignore("Property", ConfigurationSource.Convention);

            CreateConvention().Apply(modelBuilder);
        }
        public virtual void Throws_when_navigation_is_not_added_or_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NavigationEntity), ConfigurationSource.Convention);

            Assert.Equal(
                CoreStrings.NavigationNotAdded(typeof(NavigationEntity).DisplayName(fullName: false), "Navigation", typeof(PrimitivePropertyEntity).Name),
                Assert.Throws<InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
        public void Throws_when_added_property_is_not_primitive_type()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Property("Property", typeof(NavigationAsProperty), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.PropertyNotMapped("Property", typeof(NonPrimitiveAsPropertyEntity).FullName),
                Assert.Throws<InvalidOperationException>(() => new PropertyMappingValidationConvention().Apply(modelBuilder)).Message);
        }
        public void Throws_when_added_property_is_not_mapped_to_store_even_if_configured_to_use_column_type()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Property("Property", typeof(long), ConfigurationSource.Convention).Relational(ConfigurationSource.Convention).ColumnType("some_int_mapping");

            Assert.Equal(CoreStrings.PropertyNotMapped("Property", typeof(NonPrimitiveAsPropertyEntity).FullName),
                Assert.Throws<InvalidOperationException>(() => new RelationalPropertyMappingValidationConvention(new TestRelationalTypeMapper()).Apply(modelBuilder)).Message);
        }
        public void NotMappedAttribute_does_not_override_configuration_from_explicit_source()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());

            var entityBuilder = modelBuilder.Entity(typeof(A), ConfigurationSource.Explicit);

            new NotMappedEntityTypeAttributeConvention().Apply(entityBuilder);

            Assert.Equal(1, modelBuilder.Metadata.EntityTypes.Count);
        }
        public void NotMappedAttribute_overrides_configuration_from_convention_source()
        {
            var modelBuilder = new InternalModelBuilder(new Model());

            var entityBuilder = modelBuilder.Entity(typeof(A), ConfigurationSource.Convention);

            new NotMappedEntityTypeAttributeConvention().Apply(entityBuilder);

            Assert.Equal(0, modelBuilder.Metadata.GetEntityTypes().Count());
        }
        public void Throws_when_added_property_is_not_mapped_to_store_even_if_configured_to_use_column_type()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Property("LongProperty", typeof(long), ConfigurationSource.Convention).Relational(ConfigurationSource.Convention).HasColumnType("some_int_mapping");

            Assert.Equal(CoreStrings.PropertyNotMapped(
                typeof(NonPrimitiveAsPropertyEntity).DisplayName(fullName: false), "LongProperty", typeof(long).DisplayName(fullName: false)),
                Assert.Throws<InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
        public virtual void Throws_when_nonprimitive_value_type_property_is_not_added_or_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveValueTypePropertyEntity), ConfigurationSource.Convention);

            Assert.Equal(
                CoreStrings.PropertyNotAdded(
                    typeof(NonPrimitiveValueTypePropertyEntity).DisplayName(fullName: false), "Property", typeof(CancellationToken).Name),
                Assert.Throws<InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
        public void Property_added_by_name_is_non_shadow_if_matches_Clr_property()
        {
            var model = new Model();
            var modelBuilder = new InternalModelBuilder(model);
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);
            var property = builder.Metadata;

            Assert.Equal(typeof(string), property.ClrType);
            Assert.False(property.IsShadowProperty);
        }
Пример #17
0
        private static InternalEntityTypeBuilder CreateInternalEntityTypeBuilder <T>()
        {
            var modelBuilder
                = new InternalModelBuilder(
                      new Model(
                          new CoreConventionSetBuilder(
                              new CoreConventionSetBuilderDependencies(
                                  new CoreTypeMapper(new CoreTypeMapperDependencies())))
                          .CreateConventionSet()));

            return(modelBuilder.Entity(typeof(T), ConfigurationSource.Explicit));
        }
        public virtual void Throws_when_keyless_type_property_is_not_added_or_ignored()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveReferenceTypePropertyEntity), ConfigurationSource.Convention);

            Assert.Equal(
                CoreStrings.PropertyNotAdded(
                    typeof(NonPrimitiveReferenceTypePropertyEntity).ShortDisplayName(),
                    nameof(NonPrimitiveReferenceTypePropertyEntity.Property),
                    typeof(ICollection <Uri>).ShortDisplayName()),
                Assert.Throws <InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
        public virtual void Throws_when_interface_type_property_is_not_added_or_ignored()
        {
            var modelBuilder = new InternalModelBuilder(new Model());

            modelBuilder.Entity(typeof(InterfaceNavigationEntity), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.InterfacePropertyNotAdded(
                             typeof(InterfaceNavigationEntity).ShortDisplayName(),
                             "Navigation",
                             typeof(IList <INavigationEntity>).ShortDisplayName()),
                         Assert.Throws <InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
        public virtual void Throws_when_added_property_is_not_of_primitive_type()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);
            entityTypeBuilder.Property("Property", typeof(NavigationAsProperty), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.PropertyNotMapped(
                typeof(NonPrimitiveAsPropertyEntity).DisplayName(fullName: false),
                "Property",
                typeof(NavigationAsProperty).DisplayName(fullName: false)),
                Assert.Throws<InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
Пример #21
0
    public virtual void QueryFilter_containing_db_set_with_not_included_type()
    {
        var modelBuilder = new InternalModelBuilder(new Model());
        Expression <Func <Blog, bool> > lambda = e => new MyContext().Set <Post>().Single().Id == e.Id;

        modelBuilder.Entity(typeof(Blog), ConfigurationSource.Explicit)
        .HasQueryFilter(lambda, ConfigurationSource.Explicit);

        Assert.Equal(
            CoreStrings.InvalidSetType(typeof(Post).ShortDisplayName()),
            Assert.Throws <InvalidOperationException>(() => RunConvention(modelBuilder)).Message);
    }
    public void Cannot_set_required_to_false_if_nonnullable()
    {
        var modelBuilder  = new InternalModelBuilder(new Model());
        var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Convention);
        var builder       = entityBuilder.Property(typeof(int), nameof(Customer.Id), ConfigurationSource.Convention);

        Assert.Null(builder.IsRequired(false, ConfigurationSource.DataAnnotation));

        Assert.Equal(
            CoreStrings.CannotBeNullable(nameof(Customer.Id), typeof(Customer).Name, "int"),
            Assert.Throws <InvalidOperationException>(() => builder.IsRequired(false, ConfigurationSource.Explicit)).Message);
    }
        private InternalEntityTypeBuilder CreateInternalEntityTypeBuilder <T>()
        {
            var conventionSet = new ConventionSet();

            conventionSet.EntityTypeAddedConventions.Add(new PropertyDiscoveryConvention(CreateTypeMapper()));

            conventionSet.EntityTypeAddedConventions.Add(new KeyDiscoveryConvention());

            var modelBuilder = new InternalModelBuilder(new Model(conventionSet));

            return(modelBuilder.Entity(typeof(T), ConfigurationSource.Explicit));
        }
        private InternalEntityTypeBuilder CreateInternalEntityBuilder <T>()
        {
            var modelBuilder  = new InternalModelBuilder(new Model());
            var entityBuilder = modelBuilder.Entity(typeof(T), ConfigurationSource.Convention);

            var context = new ConventionContext <IConventionEntityTypeBuilder>(modelBuilder.Metadata.ConventionDispatcher);

            new PropertyDiscoveryConvention(CreateDependencies())
            .ProcessEntityTypeAdded(entityBuilder, context);

            return(entityBuilder);
        }
Пример #25
0
        public void Throws_when_added_property_is_not_mapped_to_store_even_if_configured_to_use_column_type()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);

            entityTypeBuilder.Property("LongProperty", typeof(Tuple <long>), ConfigurationSource.Convention).Relational(ConfigurationSource.Convention).HasColumnType("some_int_mapping");

            Assert.Equal(
                CoreStrings.PropertyNotMapped(
                    typeof(NonPrimitiveAsPropertyEntity).ShortDisplayName(), "LongProperty", typeof(Tuple <long>).ShortDisplayName()),
                Assert.Throws <InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
Пример #26
0
        public void Does_not_throw_an_exception_when_key_is_referenced_by_foreign_key_and_defined_on_shadow_properties_which_are_not_defined_by_convention()
        {
            var modelBuilder            = new InternalModelBuilder(new Model());
            var principalEntityBuilder  = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);
            var referencedEntityBuilder = modelBuilder.Entity(typeof(ReferencedEntity), ConfigurationSource.Convention);

            principalEntityBuilder.Property("Foo", typeof(string), ConfigurationSource.DataAnnotation);
            referencedEntityBuilder.Property("Foo", typeof(string), ConfigurationSource.DataAnnotation);
            var properties = new List <string> {
                "Foo"
            };

            referencedEntityBuilder.HasForeignKey(
                principalEntityBuilder,
                referencedEntityBuilder.GetOrCreateProperties(properties, ConfigurationSource.Convention),
                ConfigurationSource.Convention)
            .HasPrincipalKey(principalEntityBuilder.GetOrCreateProperties(properties, ConfigurationSource.Convention),
                             ConfigurationSource.Convention);

            Assert.Same(modelBuilder, new KeyConvention().Apply(modelBuilder));
        }
Пример #27
0
        public void Does_not_throw_an_exception_when_key_is_defined_on_shadow_properties_which_are_not_defined_by_convention()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);

            entityTypeBuilder.Property("Foo", typeof(string), ConfigurationSource.DataAnnotation);
            entityTypeBuilder.HasKey(new List <string> {
                "Foo"
            }, ConfigurationSource.Convention);

            Assert.Same(modelBuilder, new KeyConvention().Apply(modelBuilder));
        }
        private static InternalEntityTypeBuilder CreateInternalEntityBuilder <T>()
        {
            var modelBuilder  = new InternalModelBuilder(new Model());
            var entityBuilder = modelBuilder.Entity(typeof(T), ConfigurationSource.Convention);

            new PropertyDiscoveryConvention(
                TestServiceFactory.Instance.Create <InMemoryTypeMappingSource>(),
                new TestLogger <DbLoggerCategory.Model, TestLoggingDefinitions>())
            .Apply(entityBuilder);

            return(entityBuilder);
        }
Пример #29
0
        public virtual void Detects_shadow_key_referenced_by_foreign_key_by_convention()
        {
            var modelBuilder           = new InternalModelBuilder(new Model());
            var dependentEntityBuilder = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);

            dependentEntityBuilder.Property("Id", typeof(int), ConfigurationSource.Convention);
            dependentEntityBuilder.PrimaryKey(
                new List <string>
            {
                "Id"
            }, ConfigurationSource.Convention);
            var principalEntityBuilder = modelBuilder.Entity(typeof(ReferencedEntity), ConfigurationSource.Convention);

            principalEntityBuilder.Property("Id", typeof(int), ConfigurationSource.Convention);
            principalEntityBuilder.PrimaryKey(
                new List <string>
            {
                "Id"
            }, ConfigurationSource.Convention);

            dependentEntityBuilder.Property("Foo", typeof(string), ConfigurationSource.Convention);
            principalEntityBuilder.Property("ReferencedFoo", typeof(string), ConfigurationSource.Convention);
            dependentEntityBuilder.HasForeignKey(
                principalEntityBuilder,
                dependentEntityBuilder.GetOrCreateProperties(
                    new List <string>
            {
                "Foo"
            }, ConfigurationSource.Convention),
                principalEntityBuilder.HasKey(new[] { "ReferencedFoo" }, ConfigurationSource.Convention).Metadata,
                ConfigurationSource.Convention);

            VerifyError(
                CoreStrings.ReferencedShadowKey(
                    typeof(SampleEntity).Name,
                    typeof(ReferencedEntity).Name,
                    "{'Foo' : string}",
                    "{'Id' : int}"),
                modelBuilder.Metadata);
        }
        public void OnKeyAdded_calls_apply_on_conventions_in_order(bool useBuilder)
        {
            var conventions = new ConventionSet();

            InternalKeyBuilder keyBuilder = null;
            var convention = new Mock <IKeyConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.NotNull(b);
                keyBuilder = new InternalKeyBuilder(b.Metadata, b.ModelBuilder);
                return(keyBuilder);
            });
            conventions.KeyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock <IKeyConvention>();

            nullConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.Same(keyBuilder, b);
                return(null);
            });
            conventions.KeyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock <IKeyConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalKeyBuilder>())).Returns <InternalKeyBuilder>(b =>
            {
                Assert.False(true);
                return(null);
            });
            conventions.KeyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);

            if (useBuilder)
            {
                Assert.Null(entityBuilder.HasKey(new List <string> {
                    "OrderId"
                }, ConfigurationSource.Convention));
            }
            else
            {
                var property = entityBuilder.Property("OrderId", ConfigurationSource.Convention).Metadata;
                property.IsNullable = false;
                Assert.Null(entityBuilder.Metadata.AddKey(property));
            }

            Assert.NotNull(keyBuilder);
        }
Пример #31
0
        private static InternalEntityTypeBuilder CreateInternalEntityBuilder <T>(params Action <InternalEntityTypeBuilder>[] onEntityAdded)
        {
            var conventions = new ConventionSet();

            if (onEntityAdded != null)
            {
                conventions.EntityTypeAddedConventions.Add(new TestModelChangeListener(onEntityAdded));
            }
            var modelBuilder  = new InternalModelBuilder(new Model(), conventions);
            var entityBuilder = modelBuilder.Entity(typeof(T), ConfigurationSource.Convention);

            return(entityBuilder);
        }
        public void OnNavigationRemoved_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalEntityTypeBuilder dependentEntityTypeBuilderFromConvention = null;
            InternalEntityTypeBuilder principalEntityBuilderFromConvention     = null;
            var convention = new Mock <INavigationRemovedConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <string>())).Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n) =>
            {
                dependentEntityTypeBuilderFromConvention = s;
                principalEntityBuilderFromConvention     = t;
                Assert.Equal(nameof(OrderDetails.Order), n);
                return(false);
            });
            conventions.NavigationRemovedConventions.Add(convention.Object);

            var extraConvention = new Mock <INavigationRemovedConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <InternalEntityTypeBuilder>(), It.IsAny <string>())).Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n) =>
            {
                Assert.False(true);
                return(false);
            });
            conventions.NavigationRemovedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var principalEntityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var dependentEntityBuilder = builder.Entity(typeof(OrderDetails), ConfigurationSource.Convention);

            var relationshipBuilder = dependentEntityBuilder.Relationship(principalEntityBuilder, nameof(OrderDetails.Order), nameof(Order.OrderDetails), ConfigurationSource.Convention);

            relationshipBuilder.DependentToPrincipal(null, ConfigurationSource.Convention);

            Assert.NotNull(relationshipBuilder);
            Assert.Same(dependentEntityTypeBuilderFromConvention, dependentEntityBuilder);
            Assert.Same(principalEntityBuilderFromConvention, principalEntityBuilder);
        }
Пример #33
0
        public void Throws_an_exception_when_shadow_key_is_defined_by_convention()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);

            entityTypeBuilder.Property("Foo", typeof(string), ConfigurationSource.Convention);
            entityTypeBuilder.HasKey(new List <string> {
                "Foo"
            }, ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.ShadowKey("{'Foo'}", typeof(SampleEntity).Name, "{'Foo'}"),
                         Assert.Throws <InvalidOperationException>(() => new KeyConvention().Apply(modelBuilder)).Message);
        }
Пример #34
0
        public virtual void Detects_non_owner_navigation_to_owned_entity_type()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);

            entityTypeBuilder.PrimaryKey(new[] { nameof(SampleEntity.Id) }, ConfigurationSource.Convention);
            var ownershipBuilder = entityTypeBuilder.Owns(
                typeof(ReferencedEntity), nameof(SampleEntity.ReferencedEntity), ConfigurationSource.Convention);
            var ownedTypeBuilder = ownershipBuilder.Metadata.DeclaringEntityType.Builder;

            ownedTypeBuilder.PrimaryKey(ownershipBuilder.Metadata.Properties.Select(p => p.Name).ToList(), ConfigurationSource.Convention);
            var anotherEntityTypeBuilder = modelBuilder.Entity(typeof(AnotherSampleEntity), ConfigurationSource.Convention);

            anotherEntityTypeBuilder.PrimaryKey(new[] { nameof(AnotherSampleEntity.Id) }, ConfigurationSource.Convention);
            anotherEntityTypeBuilder.Navigation(ownedTypeBuilder, nameof(AnotherSampleEntity.ReferencedEntity), ConfigurationSource.Convention)
            .RelatedEntityTypes(anotherEntityTypeBuilder.Metadata, ownedTypeBuilder.Metadata, ConfigurationSource.Convention);

            VerifyError(
                CoreStrings.InverseToOwnedType(
                    nameof(AnotherSampleEntity), nameof(SampleEntity.ReferencedEntity), nameof(ReferencedEntity), nameof(SampleEntity)),
                modelBuilder.Metadata);
        }
Пример #35
0
        public void Throws_when_added_property_is_not_mapped_to_store()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);

            entityTypeBuilder.Property(typeof(Tuple <long>), "LongProperty", ConfigurationSource.Explicit);
            entityTypeBuilder.Ignore(nameof(NonPrimitiveAsPropertyEntity.Property), ConfigurationSource.Explicit);

            Assert.Equal(
                CoreStrings.PropertyNotMapped(
                    typeof(NonPrimitiveAsPropertyEntity).ShortDisplayName(), "LongProperty", typeof(Tuple <long>).ShortDisplayName()),
                Assert.Throws <InvalidOperationException>(() => CreatePropertyMappingValidator()(modelBuilder.Metadata)).Message);
        }
        private void ApplyOnRelatedEntityTypes(InternalModelBuilder modelBuilder, EntityType entityType)
        {
            var relatedEntityTypes = entityType.GetReferencingForeignKeys().Select(fk => fk.DeclaringEntityType)
                                     .Concat(entityType.GetForeignKeys().Select(fk => fk.PrincipalEntityType))
                                     .Distinct()
                                     .ToList();

            foreach (var relatedEntityType in relatedEntityTypes)
            {
                var relatedEntityTypeBuilder = modelBuilder.Entity(relatedEntityType.Name, ConfigurationSource.Convention);
                Apply(relatedEntityTypeBuilder);
            }
        }
Пример #37
0
        public virtual void Throws_when_added_property_is_not_of_primitive_type()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonPrimitiveAsPropertyEntity), ConfigurationSource.Convention);

            entityTypeBuilder.Property("Property", typeof(NavigationAsProperty), ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.PropertyNotMapped(
                             typeof(NonPrimitiveAsPropertyEntity).ShortDisplayName(),
                             "Property",
                             typeof(NavigationAsProperty).ShortDisplayName()),
                         Assert.Throws <InvalidOperationException>(() => CreateConvention().Apply(modelBuilder)).Message);
        }
Пример #38
0
        public void ReferencedKey_does_not_return_same_instance_if_no_navigations_or_foreign_key()
        {
            var modelBuilder          = new InternalModelBuilder(new Model(), null);
            var customerEntityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);

            customerEntityBuilder.Key(new[] { Customer.IdProperty, Customer.UniqueProperty }, ConfigurationSource.Explicit);
            var orderEntityBuilder = modelBuilder.Entity(typeof(Order), ConfigurationSource.Explicit);

            orderEntityBuilder.Key(new[] { Order.IdProperty }, ConfigurationSource.Explicit);

            var relationshipBuilder = orderEntityBuilder.Relationship(typeof(Customer), typeof(Order), null, null, ConfigurationSource.DataAnnotation, true, true)
                                      .ReferencedKey(typeof(Order), new[] { Order.CustomerIdProperty.Name, Order.CustomerUniqueProperty.Name }, ConfigurationSource.DataAnnotation);

            Assert.NotNull(relationshipBuilder);
            Assert.Same(relationshipBuilder,
                        relationshipBuilder.ReferencedKey(typeof(Order), new[] { Order.CustomerIdProperty.Name, Order.CustomerUniqueProperty.Name }, ConfigurationSource.DataAnnotation));
            Assert.NotSame(relationshipBuilder, customerEntityBuilder.Relationship(typeof(Order), typeof(Customer), null, null, ConfigurationSource.DataAnnotation, true, true)
                           .ReferencedKey(typeof(Order).FullName, new[] { Order.CustomerIdProperty.Name, Order.CustomerUniqueProperty.Name }, ConfigurationSource.Convention));
            Assert.Null(relationshipBuilder.ReferencedKey(typeof(Order), new[] { Order.CustomerIdProperty }, ConfigurationSource.Convention));

            Assert.Equal(2, customerEntityBuilder.Metadata.ForeignKeys.Count);
        }
Пример #39
0
        public void Can_only_override_lower_source_Required()
        {
            var modelBuilder          = new InternalModelBuilder(new Model(), null);
            var customerEntityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);

            customerEntityBuilder.Key(new[] { Customer.IdProperty, Customer.UniqueProperty }, ConfigurationSource.Explicit);
            var orderEntityBuilder = modelBuilder.Entity(typeof(Order), ConfigurationSource.Explicit);

            var relationshipBuilder = orderEntityBuilder.Relationship(typeof(Customer), typeof(Order), null, null, ConfigurationSource.Convention, true, true);

            Assert.Null(relationshipBuilder.Metadata.IsRequired);
            Assert.False(((IForeignKey)relationshipBuilder.Metadata).IsRequired);

            Assert.True(relationshipBuilder.Required(true, ConfigurationSource.Convention));
            Assert.True(((IForeignKey)relationshipBuilder.Metadata).IsRequired);

            Assert.True(relationshipBuilder.Required(false, ConfigurationSource.DataAnnotation));
            Assert.False(((IForeignKey)relationshipBuilder.Metadata).IsRequired);

            Assert.False(relationshipBuilder.Required(true, ConfigurationSource.Convention));
            Assert.False(((IForeignKey)relationshipBuilder.Metadata).IsRequired);
        }
        public void Property_added_by_name_is_shadow_even_if_matches_Clr_type()
        {
            var model         = new Model();
            var modelBuilder  = new InternalModelBuilder(model);
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder       = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);
            var property      = builder.Metadata;

            Assert.Equal(typeof(string), property.ClrType);
            Assert.True(property.IsShadowProperty);
            Assert.Null(property.GetClrTypeConfigurationSource());
            Assert.Null(property.GetIsShadowPropertyConfigurationSource());
        }
        public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
            {
                var unmappedProperty = entityType.GetProperties().FirstOrDefault(p => !IsMappedPrimitiveProperty(((IProperty)p).ClrType));
                if (unmappedProperty != null)
                {
                    throw new InvalidOperationException(CoreStrings.PropertyNotMapped(unmappedProperty.Name, entityType.Name));
                }

                if (entityType.HasClrType())
                {
                    var clrProperties = new HashSet<string>();
                    clrProperties.UnionWith(entityType.ClrType.GetRuntimeProperties()
                        .Where(pi => pi.IsCandidateProperty())
                        .Select(pi => pi.Name));

                    clrProperties.ExceptWith(entityType.GetProperties().Select(p => p.Name));

                    clrProperties.ExceptWith(entityType.GetNavigations().Select(p => p.Name));

                    var entityTypeBuilder = modelBuilder.Entity(entityType.ClrType, ConfigurationSource.Convention);

                    clrProperties.RemoveWhere(p => entityTypeBuilder.IsIgnored(p, ConfigurationSource.Convention));

                    if (clrProperties.Count > 0)
                    {
                        foreach (var clrProperty in clrProperties)
                        {
                            var actualProperty = entityType.ClrType.GetRuntimeProperty(clrProperty);
                            var targetType = FindCandidateNavigationPropertyType(actualProperty);
                            if (targetType != null)
                            {
                                if (!modelBuilder.IsIgnored(targetType.DisplayName(), ConfigurationSource.Convention))
                                {
                                    throw new InvalidOperationException(CoreStrings.NavigationNotAdded(actualProperty.Name, entityType.Name));
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException(CoreStrings.PropertyNotAdded(actualProperty.Name, entityType.Name));
                            }
                        }
                    }
                }
            }

            return modelBuilder;
        }
Пример #42
0
        public void OnEntityTypeIgnored_calls_apply_on_conventions_in_order(bool useBuilder)
        {
            var conventions = new ConventionSet();

            InternalModelBuilder newModelBuilder = null;
            var convention = new Mock <IEntityTypeIgnoredConvention>();

            convention.Setup(c => c.Apply(It.IsAny <InternalModelBuilder>(), It.IsAny <string>(), It.IsAny <Type>()))
            .Returns <InternalModelBuilder, string, Type>((b, n, t) =>
            {
                Assert.NotNull(b);
                Assert.Equal(typeof(Order).DisplayName(), n);
                Assert.Same(typeof(Order), t);
                return(true);
            });
            conventions.EntityTypeIgnoredConventions.Add(convention.Object);

            var haltingConvention = new Mock <IEntityTypeIgnoredConvention>();

            haltingConvention.Setup(c => c.Apply(It.IsAny <InternalModelBuilder>(), It.IsAny <string>(), It.IsAny <Type>()))
            .Returns <InternalModelBuilder, string, Type>((b, n, t) =>
            {
                newModelBuilder = new InternalModelBuilder(b.Metadata);
                return(false);
            });
            conventions.EntityTypeIgnoredConventions.Add(haltingConvention.Object);

            var extraConvention = new Mock <IEntityTypeIgnoredConvention>();

            extraConvention.Setup(c => c.Apply(It.IsAny <InternalModelBuilder>(), It.IsAny <string>(), It.IsAny <Type>()))
            .Returns <InternalModelBuilder, string, Type>((b, n, t) =>
            {
                Assert.False(true);
                return(false);
            });
            conventions.EntityTypeIgnoredConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            if (useBuilder)
            {
                builder.Entity(typeof(Order), ConfigurationSource.Convention);
                builder.Ignore(typeof(Order).DisplayName(), ConfigurationSource.Convention);
            }
            else
            {
                builder.Metadata.Ignore(typeof(Order), ConfigurationSource.Convention);
            }

            Assert.NotNull(newModelBuilder);
        }
Пример #43
0
        public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
            {
                var unmappedProperty = entityType.GetProperties().FirstOrDefault(p => !IsMappedPrimitiveProperty(((IProperty)p).ClrType));
                if (unmappedProperty != null)
                {
                    throw new InvalidOperationException(CoreStrings.PropertyNotMapped(unmappedProperty.Name, entityType.Name));
                }

                if (entityType.HasClrType())
                {
                    var clrProperties = new HashSet <string>();
                    clrProperties.UnionWith(entityType.ClrType.GetRuntimeProperties()
                                            .Where(pi => pi.IsCandidateProperty())
                                            .Select(pi => pi.Name));

                    clrProperties.ExceptWith(entityType.GetProperties().Select(p => p.Name));

                    clrProperties.ExceptWith(entityType.GetNavigations().Select(p => p.Name));

                    var entityTypeBuilder = modelBuilder.Entity(entityType.ClrType, ConfigurationSource.Convention);

                    clrProperties.RemoveWhere(p => entityTypeBuilder.IsIgnored(p, ConfigurationSource.Convention));

                    if (clrProperties.Count > 0)
                    {
                        foreach (var clrProperty in clrProperties)
                        {
                            var actualProperty = entityType.ClrType.GetRuntimeProperty(clrProperty);
                            var targetType     = FindCandidateNavigationPropertyType(actualProperty);
                            if (targetType != null)
                            {
                                if (!modelBuilder.IsIgnored(targetType.DisplayName(), ConfigurationSource.Convention))
                                {
                                    throw new InvalidOperationException(CoreStrings.NavigationNotAdded(actualProperty.Name, entityType.Name));
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException(CoreStrings.PropertyNotAdded(actualProperty.Name, entityType.Name));
                            }
                        }
                    }
                }
            }

            return(modelBuilder);
        }
Пример #44
0
        public virtual void Pases_on_valid_owned_entity_types()
        {
            var modelBuilder      = new InternalModelBuilder(new Model());
            var entityTypeBuilder = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);

            entityTypeBuilder.PrimaryKey(new[] { nameof(SampleEntity.Id) }, ConfigurationSource.Convention);
            var ownershipBuilder = entityTypeBuilder.Owns(
                typeof(ReferencedEntity), nameof(SampleEntity.ReferencedEntity), ConfigurationSource.Convention);
            var ownedTypeBuilder = ownershipBuilder.Metadata.DeclaringEntityType.Builder;

            ownedTypeBuilder.PrimaryKey(ownershipBuilder.Metadata.Properties.Select(p => p.Name).ToList(), ConfigurationSource.Convention);

            Validate(modelBuilder.Metadata);
        }
Пример #45
0
        public void Throws_an_exception_when_shadow_key_is_referenced_by_foreign_key()
        {
            var modelBuilder            = new InternalModelBuilder(new Model());
            var principalEntityBuilder  = modelBuilder.Entity(typeof(SampleEntity), ConfigurationSource.Convention);
            var referencedEntityBuilder = modelBuilder.Entity(typeof(ReferencedEntity), ConfigurationSource.Convention);

            referencedEntityBuilder.Property("Foo", typeof(string), ConfigurationSource.Convention);
            var properties = new List <string> {
                "Foo"
            };

            referencedEntityBuilder.HasForeignKey(
                principalEntityBuilder,
                referencedEntityBuilder.GetOrCreateProperties(properties, ConfigurationSource.Convention),
                ConfigurationSource.Convention);

            Assert.Equal(CoreStrings.ReferencedShadowKeyWithoutNavigations(
                             "{'Foo'}",
                             typeof(SampleEntity).Name,
                             "{'Foo'}",
                             typeof(ReferencedEntity).Name),
                         Assert.Throws <InvalidOperationException>(() => new KeyConvention().Apply(modelBuilder)).Message);
        }
        private static InternalModelBuilder BuildModel()
        {
            var modelBuilder = new InternalModelBuilder(new Model());

            var principalType = modelBuilder.Entity(typeof(PrincipalEntity), ConfigurationSource.Explicit);

            principalType.PrimaryKey(new[] { nameof(PrincipalEntity.PeeKay) }, ConfigurationSource.Explicit);

            var dependentType = modelBuilder.Entity(typeof(DependentEntity), ConfigurationSource.Explicit);

            dependentType.PrimaryKey(new[] { nameof(DependentEntity.KayPee) }, ConfigurationSource.Explicit);

            var principalTypeWithCompositeKey = modelBuilder.Entity(typeof(PrincipalEntityWithCompositeKey), ConfigurationSource.Explicit);

            principalTypeWithCompositeKey.PrimaryKey(new[] { PrincipalEntityWithCompositeKey.IdProperty, PrincipalEntityWithCompositeKey.NameProperty }, ConfigurationSource.Explicit);
            principalTypeWithCompositeKey.Property(PrincipalEntityWithCompositeKey.NameProperty, ConfigurationSource.Explicit).IsRequired(true, ConfigurationSource.Explicit);

            var dependentTypeWithCompositeKey = modelBuilder.Entity(typeof(DependentEntityWithCompositeKey), ConfigurationSource.Explicit);

            dependentTypeWithCompositeKey.PrimaryKey(new[] { nameof(DependentEntityWithCompositeKey.NotId), nameof(DependentEntityWithCompositeKey.NotName) }, ConfigurationSource.Explicit);

            return(modelBuilder);
        }
Пример #47
0
    public virtual void QueryFilter_containing_db_set_of_incorrect_type()
    {
        var modelBuilder = new InternalModelBuilder(new Model());

        modelBuilder.SharedTypeEntity("Post1", typeof(Post), ConfigurationSource.Explicit);
        Expression <Func <Blog, bool> > lambda = e => new MyContext().Set <Blog>("Post1").Single().Id == e.Id;

        modelBuilder.Entity(typeof(Blog), ConfigurationSource.Explicit)
        .HasQueryFilter(lambda, ConfigurationSource.Explicit);

        Assert.Equal(
            CoreStrings.DbSetIncorrectGenericType("Post1", typeof(Post).ShortDisplayName(), typeof(Blog).ShortDisplayName()),
            Assert.Throws <InvalidOperationException>(() => RunConvention(modelBuilder)).Message);
    }
        private InternalEntityTypeBuilder CreateInternalEntityTypeBuilder <T>()
        {
            var conventionSet = new ConventionSet();

            conventionSet.EntityTypeAddedConventions.Add(
                new PropertyDiscoveryConvention(
                    new TestRelationalTypeMappingSource(
                        TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                        TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>())));

            var modelBuilder = new InternalModelBuilder(new Model(conventionSet));

            return(modelBuilder.Entity(typeof(T), ConfigurationSource.Explicit));
        }
        public void Can_only_override_existing_ClrType_value_explicitly()
        {
            var model = new Model();
            model.AddEntityType(typeof(Customer)).AddProperty(Customer.NameProperty);
            var modelBuilder = new InternalModelBuilder(model, new ConventionSet());
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
            var builder = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);

            Assert.True(builder.ClrType(typeof(string), ConfigurationSource.DataAnnotation));
            Assert.False(builder.ClrType(typeof(int), ConfigurationSource.DataAnnotation));

            Assert.Equal(typeof(string), builder.Metadata.ClrType);

            Assert.True(builder.ClrType(typeof(int), ConfigurationSource.Explicit));
            Assert.Equal(typeof(int), builder.Metadata.ClrType);
        }
        public void OnEntityTypeAdded_calls_apply_on_conventions_in_order(bool useBuilder)
        {
            var conventions = new ConventionSet();

            InternalEntityTypeBuilder entityTypeBuilder = null;
            var convention = new Mock<IEntityTypeConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b =>
                {
                    Assert.NotNull(b);
                    entityTypeBuilder = new InternalEntityTypeBuilder(b.Metadata, b.ModelBuilder);
                    return entityTypeBuilder;
                });
            conventions.EntityTypeAddedConventions.Add(convention.Object);

            var nullConvention = new Mock<IEntityTypeConvention>();
            nullConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b =>
                {
                    Assert.Same(entityTypeBuilder, b);
                    return null;
                });
            conventions.EntityTypeAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock<IEntityTypeConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b =>
                {
                    Assert.False(true);
                    return null;
                });
            conventions.EntityTypeAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(conventions));

            if (useBuilder)
            {
                Assert.Null(builder.Entity(typeof(Order), ConfigurationSource.Convention));
            }
            else
            {
                Assert.Null(builder.Metadata.AddEntityType(typeof(Order), ConfigurationSource.Convention));
            }

            Assert.NotNull(entityTypeBuilder);
        }
        public void OnPropertyAdded_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalPropertyBuilder propertyBuilder = null;
            var convention = new Mock<IPropertyConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b =>
                {
                    Assert.NotNull(b);
                    propertyBuilder = new InternalPropertyBuilder(b.Metadata, b.ModelBuilder, ConfigurationSource.Convention);
                    return propertyBuilder;
                });
            conventions.PropertyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock<IPropertyConvention>();
            nullConvention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b =>
                {
                    Assert.Same(propertyBuilder, b);
                    return null;
                });
            conventions.PropertyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock<IPropertyConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b =>
                {
                    Assert.False(true);
                    return null;
                });
            conventions.PropertyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var explicitKeyBuilder = entityBuilder.Property(typeof(int), "OrderId", ConfigurationSource.Convention);

            Assert.Null(explicitKeyBuilder);
            Assert.NotNull(propertyBuilder);
        }
Пример #52
0
        public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
        {
            foreach (var entityType in modelBuilder.Metadata.EntityTypes)
            {
                var entityTypeBuilder = modelBuilder.Entity(entityType.Name, ConfigurationSource.Convention);
                foreach (var key in entityType.GetDeclaredKeys())
                {
                    if (key.Properties.Any(p => ((IProperty)p).IsShadowProperty && entityTypeBuilder.CanRemoveProperty(p, ConfigurationSource.Convention)))
                    {
                        string message;
                        var referencingFk = modelBuilder.Metadata.FindReferencingForeignKeys(key).FirstOrDefault();
                        if (referencingFk != null)
                        {
                            message = CoreStrings.ReferencedShadowKey(
                                Property.Format(key.Properties),
                                entityType.Name,
                                Property.Format(key.Properties),
                                Property.Format(referencingFk.Properties),
                                referencingFk.DeclaringEntityType.Name);
                        }
                        else
                        {
                            message = CoreStrings.ShadowKey(
                                Property.Format(key.Properties),
                                entityType.Name,
                                Property.Format(key.Properties));
                        }

                        throw new InvalidOperationException(message);
                    }
                }
            }

            return modelBuilder;
        }
        public void OnForeignKeyAdded_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalRelationshipBuilder relationshipBuilder = null;
            var convention = new Mock<IForeignKeyConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b =>
                {
                    Assert.NotNull(b);
                    relationshipBuilder = new InternalRelationshipBuilder(b.Metadata, b.ModelBuilder, null);
                    return relationshipBuilder;
                });
            conventions.ForeignKeyAddedConventions.Add(convention.Object);

            var nullConvention = new Mock<IForeignKeyConvention>();
            nullConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b =>
                {
                    Assert.Same(relationshipBuilder, b);
                    return null;
                });
            conventions.ForeignKeyAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock<IForeignKeyConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b =>
                {
                    Assert.False(true);
                    return null;
                });
            conventions.ForeignKeyAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention);
            Assert.Null(entityBuilder.Relationship(typeof(Order), typeof(Order), null, null, ConfigurationSource.Convention));

            Assert.NotNull(relationshipBuilder);
        }
        public void OnForeignKeyRemoved_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            var foreignKeyRemoved = false;

            var convention = new Mock<IForeignKeyRemovedConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<ForeignKey>())).Callback(() => foreignKeyRemoved = true);
            conventions.ForeignKeyRemovedConventions.Add(convention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            var foreignKey = new ForeignKey(
                new[] { entityBuilder.Property("FK", typeof(int), ConfigurationSource.Convention).Metadata },
                entityBuilder.HasKey(new[] { "OrderId" }, ConfigurationSource.Convention).Metadata,
                entityBuilder.Metadata,
                entityBuilder.Metadata);
            var conventionDispatcher = new ConventionDispatcher(conventions);
            conventionDispatcher.OnForeignKeyRemoved(entityBuilder, foreignKey);

            Assert.True(foreignKeyRemoved);
        }
        public void OnNavigationAdded_calls_apply_on_conventions_in_order()
        {
            var conventions = new ConventionSet();

            InternalRelationshipBuilder relationshipBuilder = null;
            var orderIgnored = false;
            var orderDetailsIgnored = false;
            var convention = new Mock<INavigationConvention>();
            convention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) =>
                {
                    Assert.NotNull(b);
                    relationshipBuilder = new InternalRelationshipBuilder(b.Metadata, b.ModelBuilder, ConfigurationSource.Convention);
                    return relationshipBuilder;
                });
            conventions.NavigationAddedConventions.Add(convention.Object);

            var nullConvention = new Mock<INavigationConvention>();
            nullConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) =>
                {
                    Assert.Same(relationshipBuilder, b);
                    if (n.Name == "Order")
                    {
                        orderIgnored = true;
                    }
                    if (n.Name == "OrderDetails")
                    {
                        orderDetailsIgnored = true;
                    }
                    return null;
                });
            conventions.NavigationAddedConventions.Add(nullConvention.Object);

            var extraConvention = new Mock<INavigationConvention>();
            extraConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) =>
                {
                    Assert.False(true);
                    return null;
                });
            conventions.NavigationAddedConventions.Add(extraConvention.Object);

            var builder = new InternalModelBuilder(new Model(), conventions);

            var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention);
            entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention);

            Assert.Null(entityBuilder.Relationship(typeof(Order), typeof(OrderDetails), "Order", "OrderDetails", ConfigurationSource.Convention, isUnique: true));

            Assert.True(orderIgnored);
            Assert.False(orderDetailsIgnored);
            Assert.NotNull(relationshipBuilder);
        }
        private static InternalModelBuilder BuildModel()
        {
            var modelBuilder = new InternalModelBuilder(new Model());

            var principalType = modelBuilder.Entity(typeof(PrincipalEntity), ConfigurationSource.Explicit);
            principalType.PrimaryKey(new[] { nameof(PrincipalEntity.PeeKay) }, ConfigurationSource.Explicit);

            var dependentType = modelBuilder.Entity(typeof(DependentEntity), ConfigurationSource.Explicit);
            dependentType.PrimaryKey(new[] { nameof(DependentEntity.KayPee) }, ConfigurationSource.Explicit);

            var principalTypeWithCompositeKey = modelBuilder.Entity(typeof(PrincipalEntityWithCompositeKey), ConfigurationSource.Explicit);
            principalTypeWithCompositeKey.PrimaryKey(new[] { PrincipalEntityWithCompositeKey.IdProperty, PrincipalEntityWithCompositeKey.NameProperty }, ConfigurationSource.Explicit);
            principalTypeWithCompositeKey.Property(PrincipalEntityWithCompositeKey.NameProperty, ConfigurationSource.Explicit).IsRequired(true, ConfigurationSource.Explicit);

            var dependentTypeWithCompositeKey = modelBuilder.Entity(typeof(DependentEntityWithCompositeKey), ConfigurationSource.Explicit);
            dependentTypeWithCompositeKey.PrimaryKey(new[] { nameof(DependentEntityWithCompositeKey.NotId), nameof(DependentEntityWithCompositeKey.NotName) }, ConfigurationSource.Explicit);

            return modelBuilder;
        }
        public void Cannot_set_required_to_false_if_nonnullable()
        {
            var modelBuilder = new InternalModelBuilder(new Model());
            var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Convention);
            var builder = entityBuilder.Property(nameof(Customer.Id), ConfigurationSource.Convention);
            builder.HasClrType(typeof(int), ConfigurationSource.Convention);

            Assert.False(builder.IsRequired(false, ConfigurationSource.DataAnnotation));

            Assert.Equal(CoreStrings.CannotBeNullable(nameof(Customer.Id), typeof(Customer).Name, typeof(int).Name),
                Assert.Throws<InvalidOperationException>(() => builder.IsRequired(false, ConfigurationSource.Explicit)).Message);
        }
        public void Does_not_throw_when_non_candidate_property_is_not_added()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NonCandidatePropertyEntity), ConfigurationSource.Convention);

            new PropertyMappingValidationConvention().Apply(modelBuilder);
        }
 private InternalPropertyBuilder CreateInternalPropertyBuilder()
 {
     var modelBuilder = new InternalModelBuilder(new Model());
     var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Convention);
     return entityBuilder.Property(Customer.NameProperty, ConfigurationSource.Convention);
 }
        public void Does_not_throw_when_clr_type_is_not_set_for_shadow_property()
        {
            var modelBuilder = new InternalModelBuilder(new Model(), new ConventionSet());
            var entityTypeBuilder = modelBuilder.Entity(typeof(NavigationAsProperty), ConfigurationSource.Convention);
            entityTypeBuilder.Property("ShadowPropertyOfNullType", ConfigurationSource.Convention);

            new PropertyMappingValidationConvention().Apply(modelBuilder);
        }