public void Can_get_property_and_can_try_get_property()
        {
            var entityType = new EntityType(typeof(Customer));

            entityType.AddProperty(Customer.IdProperty);

            Assert.Equal("Id", entityType.TryGetProperty("Id").Name);
            Assert.Equal("Id", entityType.GetProperty("Id").Name);

            Assert.Null(entityType.TryGetProperty("Nose"));

            Assert.Equal(
                Strings.FormatPropertyNotFound("Nose", "Customer"),
                Assert.Throws <ModelItemNotFoundException>(() => entityType.GetProperty("Nose")).Message);
        }
        public void Members_check_arguments()
        {
            Assert.Equal(
                "type",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws <ArgumentNullException>(() => new EntityType((Type)null)).ParamName);

            Assert.Equal(
                "name",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws <ArgumentNullException>(() => new EntityType((string)null)).ParamName);

            var entityType = new EntityType(typeof(Random));

            Assert.Equal(
                "propertyInfo",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws <ArgumentNullException>(() => entityType.AddProperty((PropertyInfo)null)).ParamName);

            Assert.Equal(
                "property",
                // ReSharper disable once AssignNullToNotNullAttribute
                Assert.Throws <ArgumentNullException>(() => entityType.RemoveProperty(null)).ParamName);

            Assert.Equal(
                Strings.FormatArgumentIsEmpty("name"),
                Assert.Throws <ArgumentException>(() => entityType.TryGetProperty("")).Message);

            Assert.Equal(
                Strings.FormatArgumentIsEmpty("name"),
                Assert.Throws <ArgumentException>(() => entityType.GetProperty("")).Message);
        }
                public virtual PropertyBuilder Property([NotNull] Expression <Func <TEntity, object> > propertyExpression)
                {
                    var propertyInfo = propertyExpression.GetPropertyAccess();

                    var property
                        = EntityType.TryGetProperty(propertyInfo.Name)
                          ?? EntityType.AddProperty(propertyInfo);

                    return(new PropertyBuilder(property));
                }
Example #4
0
                public IndexBuilder Index([NotNull] Expression <Func <TEntity, object> > indexExpression)
                {
                    Check.NotNull(indexExpression, "indexExpression");

                    var properties
                        = indexExpression.GetPropertyAccessList()
                          .Select(pi => EntityType.TryGetProperty(pi.Name) ?? EntityType.AddProperty(pi))
                          .ToArray();
                    var index = EntityType.AddIndex(properties);

                    return(new IndexBuilder(index));
                }
Example #5
0
                public virtual KeyMetadataBuilder Properties([NotNull] Expression <Func <TEntity, object> > keyExpression)
                {
                    Check.NotNull(keyExpression, "keyExpression");

                    EntityType.SetKey(
                        keyExpression.GetPropertyAccessList()
                        .Select(pi => EntityType.TryGetProperty(pi.Name)
                                ?? EntityType.AddProperty(pi))
                        .ToArray());

                    return(new KeyMetadataBuilder(EntityType.GetKey()));
                }
                public RelationshipBuilder ForeignKey(IList <PropertyInfo> propertyAccessList)
                {
                    var dependentProperties = propertyAccessList
                                              .Select(pi => _dependentType.TryGetProperty(pi.Name) ?? _dependentType.AddProperty(pi))
                                              .ToArray();

                    var foreignKey = Metadata;

                    if (!foreignKey.Properties.SequenceEqual(dependentProperties))
                    {
                        foreignKey = new ForeignKeyConvention().FindOrCreateForeignKey(
                            _principalType,
                            _dependentType,
                            _navigationToPrincipal != null ? _navigationToPrincipal.Name : null,
                            _navigationToDependent != null ? _navigationToDependent.Name : null,
                            new[] { dependentProperties },
                            Metadata.IsUnique);

                        // TODO: Remove FK only if it was added by convention
                        Metadata.EntityType.RemoveForeignKey(Metadata);

                        // TODO: Remove property only if it was added by convention
                        foreach (var property in Metadata.Properties.Except(dependentProperties))
                        {
                            // TODO: This check not needed once only removing properties added by convention
                            var dependentPk = Metadata.EntityType.TryGetKey();
                            if (dependentPk == null || !dependentPk.Properties.Contains(property))
                            {
                                Metadata.EntityType.RemoveProperty(property);
                            }
                        }

                        if (_navigationToPrincipal != null)
                        {
                            _navigationToPrincipal.ForeignKey = foreignKey;
                        }

                        if (_navigationToDependent != null)
                        {
                            _navigationToDependent.ForeignKey = foreignKey;
                        }
                    }

                    return(new RelationshipBuilder(
                               foreignKey, ModelBuilder, _principalType, _dependentType, _navigationToPrincipal, _navigationToDependent));
                }
                public ForeignKeyBuilder ForeignKey <TReferencedEntityType>(
                    [NotNull] Expression <Func <TEntity, object> > foreignKeyExpression)
                {
                    Check.NotNull(foreignKeyExpression, "foreignKeyExpression");

                    var principalType = ModelBuilder.Entity <TReferencedEntityType>().Metadata;

                    var dependentProperties
                        = foreignKeyExpression.GetPropertyAccessList()
                          .Select(pi => EntityType.TryGetProperty(pi.Name) ?? EntityType.AddProperty(pi))
                          .ToArray();

                    // TODO: This code currently assumes that the FK maps to a PK on the principal end
                    var foreignKey
                        = EntityType.AddForeignKey(principalType.GetKey(), dependentProperties);

                    return(new ForeignKeyBuilder(foreignKey));
                }