public void Constructor_throws_when_principal_and_depedent_property_count_do_not_match()
        {
            var dependentType = new Model().AddEntityType("D");
            var principalType = new Model().AddEntityType("P");

            var dependentProperty1 = dependentType.AddProperty("P1", typeof(int));
            var dependentProperty2 = dependentType.AddProperty("P2", typeof(int));

            var idProperty = principalType.AddProperty("Id", typeof(int));
            principalType.GetOrSetPrimaryKey(idProperty);

            Assert.Equal(
                CoreStrings.ForeignKeyCountMismatch("{'P1', 'P2'}", "D", "{'Id'}", "P"),
                Assert.Throws<InvalidOperationException>(
                    () => new ForeignKey(new[] { dependentProperty1, dependentProperty2 }, principalType.GetPrimaryKey(), dependentType, principalType)).Message);
        }
        public void Delegate_getter_is_returned_for_IProperty_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty("Id", typeof(int));
            idProperty.IsShadowProperty = false;

            Assert.Equal(7, new ClrPropertyGetterSource().GetAccessor(idProperty).GetClrValue(new Customer { Id = 7 }));
        }
        public void M_underscore_matching_field_is_not_used_if_type_is_not_compatible()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSideOfTheMoon));
            var property = entityType.AddProperty("SpeakToMe", typeof(int));
            property.IsShadowProperty = false;
            var propertyInfo = entityType.ClrType.GetAnyProperty("SpeakToMe");
            var fields = propertyInfo.DeclaringType.GetRuntimeFields().ToDictionary(f => f.Name);

            Assert.Null(new FieldMatcher().TryMatchFieldName(property, propertyInfo, fields));
        }
        public void Can_create_foreign_key()
        {
            var entityType = new Model().AddEntityType("E");
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(principalProp);

            var foreignKey
                = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey(), entityType, entityType)
                    {
                        IsUnique = true
                    };

            Assert.Same(entityType, foreignKey.PrincipalEntityType);
            Assert.Same(principalProp, foreignKey.PrincipalKey.Properties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.True(foreignKey.IsUnique.Value);
            Assert.Same(entityType.GetPrimaryKey(), foreignKey.PrincipalKey);
        }
        public void Throws_if_no_match_found_and_no_property_setter()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.TimeProperty);

            Assert.Equal(
                CoreStrings.NoFieldOrSetter(typeof(TheDarkSide).FullName, "Time"),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
        public void Delegate_setter_can_set_reference_type_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.ContentProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterSource().GetAccessor(idProperty).SetClrValue(customer, "MyString");

            Assert.Equal("MyString", customer.Content);
        }
        public void Delegate_setter_can_set_value_type_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.IdProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterSource().GetAccessor(idProperty).SetClrValue(customer, 1);

            Assert.Equal(1, customer.Id);
        }
        public void Delegate_setter_is_returned_for_IProperty_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.IdProperty);

            var customer = new Customer { Id = 7 };

            new ClrPropertySetterSource().GetAccessor(idProperty).SetClrValue(customer, 77);

            Assert.Equal(77, customer.Id);
        }
        public void Delegate_setter_is_cached_by_type_and_property_name()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty(Customer.IdProperty);

            var source = new ClrPropertySetterSource();

            var accessor = source.GetAccessor(typeof(Customer), "Id");
            Assert.Same(accessor, source.GetAccessor(typeof(Customer), "Id"));
            Assert.Same(accessor, source.GetAccessor(idProperty));
        }
        public void Field_name_match_is_used_in_preference_to_property_setter()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.BreatheProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<FieldInfo>(mapping.Item2);
            Assert.Equal("_breathe", mapping.Item2.Name);
        }
        public void Property_setter_is_used_if_no_matching_field_is_found()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.OnTheRunProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<PropertyInfo>(mapping.Item2);
            Assert.Equal("OnTheRun", mapping.Item2.Name);
        }
        public void Throws_if_annotated_field_if_types_not_compatible()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "_badFieldForSpeak";

            Assert.Equal(
                CoreStrings.BadBackingFieldType("_badFieldForSpeak", typeof(string).Name, typeof(TheDarkSide).FullName, "SpeakToMe", typeof(int).Name),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
        public void Throws_if_annotated_field_name_is_not_found()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "_speakToMe";

            Assert.Equal(
                CoreStrings.MissingBackingField(typeof(TheDarkSide).FullName, "SpeakToMe", "_speakToMe"),
                Assert.Throws<InvalidOperationException>(
                    () => new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType)).Message);
        }
        public void Annotated_field_name_is_used_if_present()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.SpeakToMeProperty);
            property["BackingField"] = "fieldForSpeak";

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.Equal("fieldForSpeak", mapping.Item2.Name);
        }
        public void Delegate_getter_is_cached_by_type_and_property_name()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));
            var idProperty = entityType.AddProperty("Id", typeof(int));
            idProperty.IsShadowProperty = false;

            var source = new ClrPropertyGetterSource();

            var accessor = source.GetAccessor(typeof(Customer), "Id");
            Assert.Same(accessor, source.GetAccessor(typeof(Customer), "Id"));
            Assert.Same(accessor, source.GetAccessor(idProperty));
        }
        private static void FieldMatchTest(string propertyName, string fieldName)
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSideOfTheMoon));
            var property = entityType.AddProperty(propertyName, typeof(int));
            property.IsShadowProperty = false;
            var propertyInfo = entityType.ClrType.GetAnyProperty(propertyName);
            var fields = propertyInfo.DeclaringType.GetRuntimeFields().ToDictionary(f => f.Name);

            var matchedField = new FieldMatcher().TryMatchFieldName(property, propertyInfo, fields);

            Assert.Equal(fieldName, matchedField.Name);
        }
        public void IsRequired_and_IsUnique_null_when_dependent_property_nullable_by_default()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(property);
            var dependentProp = entityType.AddProperty("P", typeof(int?));

            var foreignKey = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey(), entityType, entityType);

            Assert.Null(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);
            Assert.Null(foreignKey.IsUnique);
            Assert.False(((IForeignKey)foreignKey).IsUnique);
        }
        public void IsRequired_false_when_dependent_property_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(property);
            var dependentProp = entityType.AddProperty("P", typeof(int?));
            dependentProp.IsNullable = true;

            var foreignKey = new ForeignKey(new[] { dependentProp }, entityType.GetPrimaryKey(), entityType, entityType);

            Assert.False(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);
        }
        public void All_properties_have_original_value_indexes_when_using_eager_original_values()
        {
            var entityType = new Model().AddEntityType(typeof(FullNotificationEntity));
            entityType.UseEagerSnapshots = true;
            entityType.AddProperty(FullNotificationEntity.NameProperty);
            entityType.AddProperty(FullNotificationEntity.IdProperty);

            Assert.Equal(0, entityType.FindProperty("Id").GetOriginalValueIndex());
            Assert.Equal(1, entityType.FindProperty("Name").GetOriginalValueIndex());

            Assert.Equal(2, entityType.OriginalValueCount());
        }
        public void Private_setter_in_base_class_of_overridden_property_is_used()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.UsAndThemProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<PropertyInfo>(mapping.Item2);
            Assert.Equal("UsAndThem", mapping.Item2.Name);
        }
        public void Setting_IsRequired_to_false_will_set_all_FK_properties_as_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id1", typeof(int));
            var property1 = entityType.AddProperty("Id2", typeof(string));
            entityType.GetOrSetPrimaryKey(new[]
                {
                    property,
                    property1
                });

            var dependentProp1 = entityType.AddProperty("P1", typeof(int?));
            var dependentProp2 = entityType.AddProperty("P2", typeof(string));

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey(), entityType, entityType) { IsRequired = false };

            Assert.False(foreignKey.IsRequired.Value);
            Assert.True(dependentProp1.IsNullable.Value);
            Assert.True(dependentProp2.IsNullable.Value);
        }
        public void Property_and_field_in_base_type_is_matched()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(OfTheMoon.TheGreatGigInTheSkyProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<FieldInfo>(mapping.Item2);
            Assert.Equal("_theGreatGigInTheSky", mapping.Item2.Name);
        }
        public void IsRequired_false_for_composite_FK_by_default()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id1", typeof(int));
            var property1 = entityType.AddProperty("Id2", typeof(string));
            entityType.GetOrSetPrimaryKey(new[]
                {
                    property,
                    property1
                });

            var dependentProp1 = entityType.AddProperty("P1", typeof(int));
            var dependentProp2 = entityType.AddProperty("P2", typeof(string));

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey(), entityType, entityType);

            Assert.Null(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);
        }
        public void AddProperty_throws_if_shadow_entity_type()
        {
            var entityType = new Model().AddEntityType("Customer");

            Assert.Equal(CoreStrings.ClrPropertyOnShadowEntity(nameof(Customer.Name), "Customer"),
                Assert.Throws<InvalidOperationException>(() =>
                    entityType.AddProperty(nameof(Customer.Name), typeof(int), shadow: false)).Message);
        }
        public void IsRequired_false_when_any_part_of_composite_FK_is_nullable()
        {
            var entityType = new Model().AddEntityType("E");
            var property = entityType.AddProperty("Id1", typeof(int));
            var property1 = entityType.AddProperty("Id2", typeof(string));
            entityType.GetOrSetPrimaryKey(new[]
                {
                    property,
                    property1
                });

            var dependentProp1 = entityType.AddProperty("P1", typeof(int));
            var dependentProp2 = entityType.AddProperty("P2", typeof(string));
            dependentProp2.IsNullable = true;

            var foreignKey = new ForeignKey(new[] { dependentProp1, dependentProp2 }, entityType.GetPrimaryKey(), entityType, entityType);

            Assert.False(foreignKey.IsRequired);
            Assert.False(((IForeignKey)foreignKey).IsRequired);

            dependentProp2.IsNullable = false;

            Assert.True(foreignKey.IsRequired);
            Assert.True(((IForeignKey)foreignKey).IsRequired);
        }
        public void AddProperty_throws_if_no_clr_property()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));

            Assert.Equal(CoreStrings.NoClrProperty("Random", nameof(Customer)),
                Assert.Throws<InvalidOperationException>(() =>
                    entityType.AddProperty("Random", typeof(int), shadow: false)).Message);
        }
        private ForeignKey CreateSelfRefFK(bool useAltKey = false)
        {
            var entityType = new Model().AddEntityType(typeof(SelfRef));
            var pk = entityType.GetOrSetPrimaryKey(entityType.AddProperty(SelfRef.IdProperty));
            var fkProp = entityType.AddProperty(SelfRef.SelfRefIdProperty);

            var property = entityType.AddProperty("AltId", typeof(int));
            var principalKey = useAltKey
                ? entityType.GetOrAddKey(property)
                : pk;

            var fk = entityType.AddForeignKey(new[] { fkProp }, principalKey, entityType);
            fk.IsUnique = true;
            entityType.AddNavigation("SelfRefPrincipal", fk, pointsToPrincipal: true);
            entityType.AddNavigation("SelfRefDependent", fk, pointsToPrincipal: false);
            return fk;
        }
        public void AddProperty_throws_if_clr_type_does_not_match()
        {
            var entityType = new Model().AddEntityType(typeof(Customer));

            Assert.Equal(CoreStrings.PropertyWrongClrType(
                nameof(Customer.Name), nameof(Customer), typeof(string).DisplayName(), typeof(int).DisplayName(fullName: false)),
                Assert.Throws<InvalidOperationException>(() =>
                    entityType.AddProperty(nameof(Customer.Name), typeof(int), shadow: false)).Message);
        }
        public void Can_change_cascade_delete_flag()
        {
            var entityType = new Model().AddEntityType("E");
            var keyProp = entityType.AddProperty("Id", typeof(int));
            var dependentProp = entityType.AddProperty("P", typeof(int));
            var principalProp = entityType.AddProperty("U", typeof(int));
            entityType.GetOrSetPrimaryKey(keyProp);
            var principalKey = entityType.AddKey(principalProp);

            var foreignKey
                = new ForeignKey(new[] { dependentProp }, principalKey, entityType, entityType);

            Assert.Null(foreignKey.DeleteBehavior);
            Assert.Equal(DeleteBehavior.None, ((IForeignKey)foreignKey).DeleteBehavior);

            foreignKey.DeleteBehavior = DeleteBehavior.Cascade;

            Assert.Equal(DeleteBehavior.Cascade, foreignKey.DeleteBehavior);
            Assert.Equal(DeleteBehavior.Cascade, ((IForeignKey)foreignKey).DeleteBehavior);

            foreignKey.DeleteBehavior = DeleteBehavior.None;

            Assert.Equal(DeleteBehavior.None, foreignKey.DeleteBehavior);
            Assert.Equal(DeleteBehavior.None, ((IForeignKey)foreignKey).DeleteBehavior);

            foreignKey.DeleteBehavior = null;

            Assert.Null(foreignKey.DeleteBehavior);
            Assert.Equal(DeleteBehavior.None, ((IForeignKey)foreignKey).DeleteBehavior);
        }
        public void Property_and_field_in_base_type_is_matched_even_when_overridden()
        {
            var entityType = new Model().AddEntityType(typeof(TheDarkSide));
            var property = entityType.AddProperty(TheDarkSide.MoneyProperty);

            var mapping = new MemberMapper(new FieldMatcher()).MapPropertiesToMembers(entityType).Single();

            Assert.Same(property, mapping.Item1);
            Assert.IsAssignableFrom<FieldInfo>(mapping.Item2);
            Assert.Equal("_money", mapping.Item2.Name);
        }