Пример #1
0
                public ForeignKeyBuilder ForeignKey <TReferencedEntityType>(
                    [NotNull] Expression <Func <TEntity, object> > foreignKeyExpression, bool isUnique = false)
                {
                    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);

                    foreignKey.IsUnique = isUnique;

                    return(new ForeignKeyBuilder(foreignKey));
                }
Пример #2
0
        public void FK_back_pointer_is_fixed_up_as_FK_is_added()
        {
            var entityType = new EntityType(typeof(Customer));
            var property   = entityType.AddProperty(Customer.IdProperty);

            entityType.SetKey(property);
            var foreignKey
                = entityType.AddForeignKey(entityType.GetKey(), property);

            Assert.Same(entityType, foreignKey.EntityType);
            Assert.Same(entityType, property.EntityType);

            entityType.RemoveForeignKey(foreignKey);

            // Currently property is not removed when FK is removed
            Assert.Empty(entityType.ForeignKeys);
            Assert.Same(property, entityType.Properties.Single());
            Assert.Same(entityType, foreignKey.EntityType); // TODO: Throw here?
            Assert.Same(entityType, property.EntityType);
        }
Пример #3
0
        public void Can_create_materializer_for_entity_ignoring_shadow_fields()
        {
            var entityType = new EntityType(typeof(SomeEntity));

            entityType.AddProperty("Id", typeof(int));
            entityType.AddProperty("IdShadow", typeof(int), shadowProperty: true, concurrencyToken: false);
            entityType.AddProperty("Foo", typeof(string));
            entityType.AddProperty("FooShadow", typeof(string), shadowProperty: true, concurrencyToken: false);
            entityType.AddProperty("Goo", typeof(Guid));
            entityType.AddProperty("GooShadow", typeof(Guid), shadowProperty: true, concurrencyToken: false);

            var factory = new EntityMaterializerSource(new MemberMapper(new FieldMatcher())).GetMaterializer(entityType);

            var gu     = Guid.NewGuid();
            var entity = (SomeEntity)factory(new ObjectArrayValueReader(new object[] { "Fu", "FuS", gu, Guid.NewGuid(), 77, 777 }));

            Assert.Equal(77, entity.Id);
            Assert.Equal("Fu", entity.Foo);
            Assert.Equal(gu, entity.Goo);
        }
Пример #4
0
                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));
                }