public ForeignKey(
            [NotNull] IReadOnlyList<Property> dependentProperties,
            [NotNull] Key principalKey,
            [CanBeNull] EntityType principalEntityType = null)
        {
            Check.NotEmpty(dependentProperties, nameof(dependentProperties));
            Check.HasNoNulls(dependentProperties, nameof(dependentProperties));
            MetadataHelper.CheckSameEntityType(dependentProperties, nameof(dependentProperties));
            Check.NotNull(principalKey, nameof(principalKey));

            Properties = dependentProperties;

            var principalProperties = principalKey.Properties;

            Property.EnsureCompatible(principalProperties, dependentProperties);

            if (principalEntityType?.Keys.Contains(principalKey) == false)
            {
                throw new ArgumentException(
                    Strings.ForeignKeyReferencedEntityKeyMismatch(
                        principalKey,
                        principalEntityType));
            }

            _principalKey = principalKey;

            PrincipalEntityType = principalEntityType ?? _principalKey.EntityType;
        }
Exemple #2
0
        internal ForeignKey([NotNull] Key referencedKey, [NotNull] IReadOnlyList<Property> dependentProperties)
            : base(Check.NotNull(dependentProperties, "dependentProperties"))
        {
            Check.NotNull(referencedKey, "referencedKey");

            _referencedKey = referencedKey;
        }
Exemple #3
0
        public void Can_create_key_from_properties()
        {
            var entityType = new EntityType("E");
            var property1 = entityType.AddProperty(Customer.IdProperty);
            var property2 = entityType.AddProperty(Customer.NameProperty);

            var key = new Key(new[] { property1, property2 });

            Assert.True(new[] { property1, property2 }.SequenceEqual(key.Properties));
        }
        private ForeignKey CreateForeignKey(Key dependentKey, Key principalKey)
        {
            var foreignKey = dependentKey.DeclaringEntityType.AddForeignKey(dependentKey.Properties, principalKey, principalKey.DeclaringEntityType);
            foreignKey.IsUnique = true;
            foreignKey.IsRequired = false;
            foreach (var property in dependentKey.Properties)
            {
                property.RequiresValueGenerator = false;
            }

            return foreignKey;
        }
        public SqlServerKeyBuilder([NotNull] Key key)
        {
            Check.NotNull(key, nameof(key));

            _key = key;
        }
Exemple #6
0
 public virtual ForeignKey GetOrAddForeignKey(
     [NotNull] Property property, [NotNull] Key principalKey)
 {
     return(GetOrAddForeignKey(new[] { property }, principalKey));
 }
Exemple #7
0
        public NpgsqlKeyBuilder([NotNull] Key key)
        {
            Check.NotNull(key, nameof(key));

            _key = key;
        }
        public void Can_create_foreign_key_with_non_pk_principal()
        {
            var entityType = new Model().AddEntityType("E");
            var keyProp = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            var dependentProp = entityType.GetOrAddProperty("P", typeof(int), shadowProperty: true);
            var principalProp = entityType.GetOrAddProperty("U", typeof(int), shadowProperty: true);
            entityType.GetOrSetPrimaryKey(keyProp);
            var principalKey = new Key(new[] { principalProp });

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

            Assert.Same(entityType, foreignKey.PrincipalEntityType);
            Assert.Same(principalProp, foreignKey.PrincipalKey.Properties.Single());
            Assert.Same(dependentProp, foreignKey.Properties.Single());
            Assert.False(foreignKey.IsUnique.Value);
            Assert.Same(principalKey, foreignKey.PrincipalKey);
        }
        public RelationalKeyBuilder([NotNull] Key key)
        {
            Check.NotNull(key, nameof(key));

            _key = key;
        }