Esempio n. 1
0
 public virtual ConventionNavigationPropertyConfiguration HasInverseNavigationProperty(
     Func <PropertyInfo, PropertyInfo> inverseNavigationPropertyGetter)
 {
     Check.NotNull <Func <PropertyInfo, PropertyInfo> >(inverseNavigationPropertyGetter, nameof(inverseNavigationPropertyGetter));
     if (this._configuration != null && this._configuration.InverseNavigationProperty == (PropertyInfo)null)
     {
         PropertyInfo propertyInfo = inverseNavigationPropertyGetter(this.ClrPropertyInfo);
         Check.NotNull <PropertyInfo>(propertyInfo, "inverseNavigationProperty");
         if (!propertyInfo.IsValidEdmNavigationProperty())
         {
             throw new InvalidOperationException(Strings.LightweightEntityConfiguration_InvalidNavigationProperty((object)propertyInfo.Name));
         }
         if (!propertyInfo.DeclaringType.IsAssignableFrom(this._configuration.NavigationProperty.PropertyType.GetTargetType()))
         {
             throw new InvalidOperationException(Strings.LightweightEntityConfiguration_MismatchedInverseNavigationProperty((object)this._configuration.NavigationProperty.PropertyType.GetTargetType().FullName, (object)this._configuration.NavigationProperty.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyInfo.Name));
         }
         if (!this._configuration.NavigationProperty.DeclaringType.IsAssignableFrom(propertyInfo.PropertyType.GetTargetType()))
         {
             throw new InvalidOperationException(Strings.LightweightEntityConfiguration_InvalidInverseNavigationProperty((object)this._configuration.NavigationProperty.DeclaringType.FullName, (object)this._configuration.NavigationProperty.Name, (object)propertyInfo.PropertyType.GetTargetType().FullName, (object)propertyInfo.Name));
         }
         if (this._configuration.InverseEndKind.HasValue)
         {
             ConventionNavigationPropertyConfiguration.VerifyMultiplicityCompatibility(this._configuration.InverseEndKind.Value, propertyInfo);
         }
         this._modelConfiguration.Entity(this._configuration.NavigationProperty.PropertyType.GetTargetType()).Navigation(propertyInfo);
         this._configuration.InverseNavigationProperty = propertyInfo;
     }
     return(this);
 }
Esempio n. 2
0
        public void ClrPropertyInfo_returns_the_propertyInfo()
        {
            var propInfo = typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty");
            var config   = new ConventionNavigationPropertyConfiguration(
                new NavigationPropertyConfiguration(propInfo), new ModelConfiguration());

            Assert.Same(propInfo, config.ClrPropertyInfo);
        }
Esempio n. 3
0
        public void HasInverseNavigationProperty_checks_preconditions()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("PrivateNavigationProperty");
            var lightweightConfiguration =
                new ConventionNavigationPropertyConfiguration(
                    new NavigationPropertyConfiguration(navigationProperty), new ModelConfiguration());

            Assert.Throws <ArgumentNullException>(
                () => lightweightConfiguration.HasInverseNavigationProperty(null));
        }
Esempio n. 4
0
        public void HasConstraint_sets_the_ForeignKeyConstraint()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasConstraint <ForeignKeyConstraintConfiguration>();
            Assert.IsType <ForeignKeyConstraintConfiguration>(configuration.Constraint);
        }
Esempio n. 5
0
 public virtual ConventionNavigationPropertyConfiguration HasRelationshipMultiplicity(
     RelationshipMultiplicity multiplicity)
 {
     if (this._configuration != null && !this._configuration.RelationshipMultiplicity.HasValue)
     {
         ConventionNavigationPropertyConfiguration.VerifyMultiplicityCompatibility(multiplicity, this._configuration.NavigationProperty);
         this._configuration.RelationshipMultiplicity = new RelationshipMultiplicity?(multiplicity);
     }
     return(this);
 }
Esempio n. 6
0
        public void HasConstraint_sets_the_IndependentConstraint()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasConstraint <IndependentConstraintConfiguration>();
            Assert.Same(IndependentConstraintConfiguration.Instance, configuration.Constraint);
        }
Esempio n. 7
0
        public void HasInverseNavigationProperty_throws_on_itself()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("PrivateNavigationProperty");
            var configuration            = new NavigationPropertyConfiguration(navigationProperty);
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            Assert.Equal(
                Strings.NavigationInverseItself(navigationProperty.Name, navigationProperty.DeclaringType),
                Assert.Throws <InvalidOperationException>(
                    () => lightweightConfiguration.HasInverseNavigationProperty(p => p)).Message);
        }
        public void HasConstraint_is_noop_when_set_to_IndependentConstraint()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetProperty("ValidNavigationProperty"));

            configuration.Constraint = IndependentConstraintConfiguration.Instance;
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasConstraint <ForeignKeyConstraintConfiguration>();
            Assert.Same(IndependentConstraintConfiguration.Instance, configuration.Constraint);
        }
Esempio n. 9
0
        public void Methods_dont_throw_if_configuration_is_null()
        {
            var config = new ConventionNavigationPropertyConfiguration(null, null);

            Assert.Null(config.ClrPropertyInfo);
            config.HasConstraint <IndependentConstraintConfiguration>();
            config.HasDeleteAction(OperationAction.Cascade);
            config.HasInverseEndMultiplicity(RelationshipMultiplicity.ZeroOrOne);
            config.HasInverseNavigationProperty(p => new MockPropertyInfo());
            config.IsDeclaringTypePrincipal(true);
            config.HasRelationshipMultiplicity(RelationshipMultiplicity.ZeroOrOne);
        }
Esempio n. 10
0
        public void HasConstraint_sets_and_configures_the_ForeignKeyConstraint()
        {
            var property      = new MockPropertyInfo();
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasConstraint <ForeignKeyConstraintConfiguration>(c => c.AddColumn(property));
            Assert.IsType <ForeignKeyConstraintConfiguration>(configuration.Constraint);
            Assert.Same(property.Object, ((ForeignKeyConstraintConfiguration)configuration.Constraint).ToProperties.Single());
        }
Esempio n. 11
0
 public virtual ConventionNavigationPropertyConfiguration HasInverseEndMultiplicity(
     RelationshipMultiplicity multiplicity)
 {
     if (this._configuration != null && !this._configuration.InverseEndKind.HasValue)
     {
         if (this._configuration.InverseNavigationProperty != (PropertyInfo)null)
         {
             ConventionNavigationPropertyConfiguration.VerifyMultiplicityCompatibility(multiplicity, this._configuration.InverseNavigationProperty);
         }
         this._configuration.InverseEndKind = new RelationshipMultiplicity?(multiplicity);
     }
     return(this);
 }
Esempio n. 12
0
        public void HasDeleteAction_sets_the_DeleteAction()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasDeleteAction(OperationAction.Cascade);
            Assert.Equal(OperationAction.Cascade, configuration.DeleteAction);

            configuration.DeleteAction = OperationAction.None;
            lightweightConfiguration.HasDeleteAction(OperationAction.Cascade);
            Assert.Equal(OperationAction.None, configuration.DeleteAction);
        }
Esempio n. 13
0
        public void HasInverseNavigationProperty_throws_on_invalid_nagivation_property()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty");
            var lightweightConfiguration =
                new ConventionNavigationPropertyConfiguration(
                    new NavigationPropertyConfiguration(navigationProperty), new ModelConfiguration());

            var inverseNavigationProperty = typeof(LightweighEntity).GetDeclaredProperty("InvalidNavigationProperty");

            Assert.Equal(
                Strings.LightweightEntityConfiguration_InvalidNavigationProperty(inverseNavigationProperty.Name),
                Assert.Throws <InvalidOperationException>(
                    () => lightweightConfiguration.HasInverseNavigationProperty(p => inverseNavigationProperty)).Message);
        }
        public void HasInverseEndMultiplicity_sets_the_InverseEndKind()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasInverseEndMultiplicity(RelationshipMultiplicity.ZeroOrOne);
            Assert.Equal(RelationshipMultiplicity.ZeroOrOne, configuration.InverseEndKind);

            configuration.InverseEndKind = RelationshipMultiplicity.Many;
            lightweightConfiguration.HasInverseEndMultiplicity(RelationshipMultiplicity.ZeroOrOne);
            Assert.Equal(RelationshipMultiplicity.Many, configuration.InverseEndKind);
        }
Esempio n. 15
0
        public void HasRelationshipMultiplicity_gets_and_sets_the_RelationshipMultiplicity()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.HasRelationshipMultiplicity(RelationshipMultiplicity.ZeroOrOne);
            Assert.Equal(RelationshipMultiplicity.ZeroOrOne, configuration.RelationshipMultiplicity);

            configuration.RelationshipMultiplicity = RelationshipMultiplicity.Many;
            lightweightConfiguration.HasRelationshipMultiplicity(RelationshipMultiplicity.ZeroOrOne);
            Assert.Equal(RelationshipMultiplicity.Many, configuration.RelationshipMultiplicity);
        }
Esempio n. 16
0
        public void IsDeclaringTypePrincipal_sets_IsNavigationPropertyDeclaringTypePrincipal()
        {
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            lightweightConfiguration.IsDeclaringTypePrincipal(false);
            Assert.Equal(false, configuration.IsNavigationPropertyDeclaringTypePrincipal);

            configuration.IsNavigationPropertyDeclaringTypePrincipal = true;
            lightweightConfiguration.IsDeclaringTypePrincipal(false);
            Assert.Equal(true, configuration.IsNavigationPropertyDeclaringTypePrincipal);
        }
Esempio n. 17
0
        public void HasInverseNavigationProperty_throws_on_nonmatching_nagivation_property()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("UnrelatedNavigationProperty");
            var configuration            = new NavigationPropertyConfiguration(navigationProperty);
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, new ModelConfiguration());

            var inverseNavigationProperty = typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty");

            Assert.Equal(
                Strings.LightweightEntityConfiguration_MismatchedInverseNavigationProperty(
                    typeof(ConventionNavigationPropertyConfigurationTests), navigationProperty.Name,
                    typeof(LightweighEntity), inverseNavigationProperty.Name),
                Assert.Throws <InvalidOperationException>(
                    () => lightweightConfiguration.HasInverseNavigationProperty(p => inverseNavigationProperty)).Message);
        }
Esempio n. 18
0
        public void HasRelationshipMultiplicity_throws_on_incompatible_multiplicity()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("PrivateNavigationProperty");
            var configuration            = new NavigationPropertyConfiguration(navigationProperty);
            var modelConfiguration       = new ModelConfiguration();
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, modelConfiguration);

            Assert.Equal(
                Strings.LightweightNavigationPropertyConfiguration_IncompatibleMultiplicity(
                    "0..1",
                    typeof(LightweighEntity).Name + "." + "PrivateNavigationProperty",
                    "System.Collections.Generic.ICollection`1[" +
                    typeof(LightweighEntity).FullName + "]"),
                Assert.Throws <InvalidOperationException>(
                    () => lightweightConfiguration.HasRelationshipMultiplicity(RelationshipMultiplicity.ZeroOrOne)).Message);
        }
Esempio n. 19
0
        public void HasConstraint_is_noop_when_set_on_inverse()
        {
            var modelConfiguration        = new ModelConfiguration();
            var inverseNavigationProperty = typeof(LightweighEntity).GetDeclaredProperty("PrivateNavigationProperty");

            modelConfiguration.Entity(typeof(LightweighEntity))
            .Navigation(inverseNavigationProperty)
            .Constraint = IndependentConstraintConfiguration.Instance;
            var configuration =
                new NavigationPropertyConfiguration(
                    typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty"));

            configuration.InverseNavigationProperty = inverseNavigationProperty;
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, modelConfiguration);

            lightweightConfiguration.HasConstraint <ForeignKeyConstraintConfiguration>();
            Assert.Null(configuration.Constraint);
        }
Esempio n. 20
0
        public void HasInverseNavigationProperty_sets_the_InverseNavigationProperty()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty");
            var configuration            = new NavigationPropertyConfiguration(navigationProperty);
            var modelConfiguration       = new ModelConfiguration();
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, modelConfiguration);

            var inverseNavigationProperty1 =
                typeof(LightweighEntity).GetDeclaredProperty("ValidInverseNavigationProperty");

            lightweightConfiguration.HasInverseNavigationProperty(p => inverseNavigationProperty1);
            Assert.Same(inverseNavigationProperty1, configuration.InverseNavigationProperty);

            var inverseNavigationProperty2 = typeof(LightweighEntity).GetDeclaredProperty("PrivateNavigationProperty");

            configuration.InverseNavigationProperty = inverseNavigationProperty2;
            lightweightConfiguration.HasInverseNavigationProperty(p => inverseNavigationProperty1);
            Assert.Same(inverseNavigationProperty2, configuration.InverseNavigationProperty);
        }
Esempio n. 21
0
        public void HasInverseNavigationProperty_throws_on_incompatible_multiplicity()
        {
            var navigationProperty       = typeof(LightweighEntity).GetDeclaredProperty("ValidNavigationProperty");
            var configuration            = new NavigationPropertyConfiguration(navigationProperty);
            var modelConfiguration       = new ModelConfiguration();
            var lightweightConfiguration = new ConventionNavigationPropertyConfiguration(configuration, modelConfiguration);

            var inverseNavigationProperty =
                typeof(LightweighEntity).GetDeclaredProperty("ValidInverseNavigationProperty");

            lightweightConfiguration.HasInverseEndMultiplicity(RelationshipMultiplicity.Many);

            Assert.Equal(
                Strings.LightweightNavigationPropertyConfiguration_IncompatibleMultiplicity(
                    "*",
                    typeof(LightweighEntity).Name + "." + "ValidInverseNavigationProperty",
                    typeof(LightweighEntity).FullName),
                Assert.Throws <InvalidOperationException>(
                    () => lightweightConfiguration.HasInverseNavigationProperty(p => inverseNavigationProperty)).Message);
        }