Exemplo n.º 1
0
 public void AddProperties(IMutableEntityType entityTypeA)
 {
     entityTypeA.AddProperty(nameof(A.P0), typeof(int?));
     entityTypeA.AddProperty(nameof(A.P1), typeof(int?));
     entityTypeA.AddProperty(nameof(A.P2), typeof(int?));
     entityTypeA.AddProperty(nameof(A.P3), typeof(int?));
 }
        /// <summary>
        ///     Gets the property with the given name, or creates a new one if one is not already defined.
        /// </summary>
        /// <param name="entityType"> The entity type to get or add the property to. </param>
        /// <param name="name"> The name of the property. </param>
        /// <returns> The existing or newly created property. </returns>
        public static IMutableProperty GetOrAddProperty(
            [NotNull] this IMutableEntityType entityType, [NotNull] string name)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.FindProperty(name) ?? entityType.AddProperty(name));
        }
        protected override void SetBaseType(IMutableEntityType entityType, IMutableEntityType baseEntityType)
        {
            base.SetBaseType(entityType, baseEntityType);

            baseEntityType.Relational().DiscriminatorProperty = baseEntityType.AddProperty("Discriminator", typeof(string));
            baseEntityType.Relational().DiscriminatorValue    = baseEntityType.Name;
            entityType.Relational().DiscriminatorValue        = entityType.Name;
        }
Exemplo n.º 4
0
        protected override void SetBaseType(IMutableEntityType entityType, IMutableEntityType baseEntityType)
        {
            base.SetBaseType(entityType, baseEntityType);

            baseEntityType.SetDiscriminatorProperty(baseEntityType.AddProperty("Discriminator", typeof(string)));
            baseEntityType.SetDiscriminatorValue(baseEntityType.Name);
            entityType.SetDiscriminatorValue(entityType.Name);
        }
        /// <summary>
        ///     Adds a property to this entity.
        /// </summary>
        /// <param name="entityType"> The entity type to add the property to. </param>
        /// <param name="name"> The name of the property to add. </param>
        /// <param name="propertyType"> The type of value the property will hold. </param>
        /// <returns> The newly created property. </returns>
        public static IMutableProperty AddProperty(
            [NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(propertyType, nameof(propertyType));

            var property = entityType.AddProperty(name);

            property.ClrType = propertyType;
            return(property);
        }
Exemplo n.º 6
0
 private static void CloneProperties(IReadOnlyEntityType sourceEntityType, IMutableEntityType targetEntityType)
 {
     foreach (var property in sourceEntityType.GetDeclaredProperties())
     {
         var clonedProperty = targetEntityType.AddProperty(property.Name, property.ClrType);
         clonedProperty.IsNullable         = property.IsNullable;
         clonedProperty.IsConcurrencyToken = property.IsConcurrencyToken;
         clonedProperty.ValueGenerated     = property.ValueGenerated;
         clonedProperty.SetBeforeSaveBehavior(property.GetBeforeSaveBehavior());
         clonedProperty.SetAfterSaveBehavior(property.GetAfterSaveBehavior());
         property.GetAnnotations().ForEach(annotation => clonedProperty[annotation.Name] = annotation.Value);
     }
 }
        /// <summary>
        ///     Adds a property backed up by an indexer to this entity type.
        /// </summary>
        /// <param name="entityType"> The entity type to add the property to. </param>
        /// <param name="name"> The name of the property to add. </param>
        /// <param name="propertyType"> The type of value the property will hold. </param>
        /// <returns> The newly created property. </returns>
        public static IMutableProperty AddIndexerProperty(
            [NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType)
        {
            Check.NotNull(entityType, nameof(entityType));

            var indexerPropertyInfo = entityType.FindIndexerPropertyInfo();

            if (indexerPropertyInfo == null)
            {
                throw new InvalidOperationException(
                          CoreStrings.NonIndexerEntityType(name, entityType.DisplayName(), typeof(string).ShortDisplayName()));
            }

            return(entityType.AddProperty(name, propertyType, indexerPropertyInfo));
        }
        /// <summary>
        ///     Gets the property with the given name, or creates a new one if one is not already defined.
        /// </summary>
        /// <param name="entityType"> The entity type to get or add the property to. </param>
        /// <param name="propertyInfo"> The corresponding property in the entity class. </param>
        /// <returns> The existing or newly created property. </returns>
        public static IMutableProperty GetOrAddProperty(
            [NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(propertyInfo, nameof(propertyInfo));

            var property = entityType.FindProperty(propertyInfo);

            if (property != null)
            {
                property.ClrType          = propertyInfo.PropertyType;
                property.IsShadowProperty = false;
                return(property);
            }

            return(entityType.AddProperty(propertyInfo));
        }
        /// <summary>
        ///     Adds a property to this entity.
        /// </summary>
        /// <param name="entityType"> The entity type to add the property to. </param>
        /// <param name="propertyInfo"> The corresponding property in the entity class. </param>
        /// <returns> The newly created property. </returns>
        public static IMutableProperty AddProperty(
            [NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(propertyInfo, nameof(propertyInfo));

            if (entityType.HasClrType() &&
                !propertyInfo.DeclaringType.GetTypeInfo().IsAssignableFrom(entityType.ClrType.GetTypeInfo()))
            {
                throw new ArgumentException(CoreStrings.PropertyWrongEntityClrType(
                                                propertyInfo.Name, entityType.DisplayName(), propertyInfo.DeclaringType.Name));
            }

            var property = entityType.AddProperty(propertyInfo.Name, propertyInfo.PropertyType);

            property.IsShadowProperty = false;
            return(property);
        }
        protected IMutableKey CreateKey(IMutableEntityType entityType, int startingPropertyIndex = -1, int propertyCount = 1)
        {
            if (startingPropertyIndex == -1)
            {
                startingPropertyIndex = entityType.PropertyCount() - 1;
            }

            var keyProperties = new IMutableProperty[propertyCount];

            for (var i = 0; i < propertyCount; i++)
            {
                var propertyName = "P" + (startingPropertyIndex + i);
                keyProperties[i] = entityType.FindProperty(propertyName)
                                   ?? entityType.AddProperty(propertyName, typeof(int?));
                keyProperties[i].IsNullable = false;
            }

            return(entityType.AddKey(keyProperties));
        }
Exemplo n.º 11
0
        public void Marking_a_property_ValueGenerated_throws_if_part_of_a_key_and_inherited_foreign_key()
        {
            var model       = new Model();
            var baseType    = model.AddEntityType(typeof(BaseType));
            var idProperty  = baseType.GetOrAddProperty(nameof(Customer.Id), typeof(int));
            var idProperty2 = baseType.GetOrAddProperty("id2", typeof(int));
            var key         = baseType.GetOrAddKey(new[] { idProperty, idProperty2 });
            IMutableEntityType entityType = model.AddEntityType(typeof(Customer));

            entityType.BaseType = baseType;
            var fkProperty = entityType.AddProperty("fk", typeof(int));

            entityType.AddForeignKey(new[] { fkProperty, idProperty }, key, entityType);

            Assert.Equal(
                CoreStrings.ForeignKeyPropertyInKey(
                    nameof(Customer.Id),
                    typeof(Customer).Name,
                    "{'" + nameof(Customer.Id) + "'" + ", 'id2'}",
                    typeof(BaseType).Name),
                Assert.Throws <InvalidOperationException>(() => idProperty.ValueGenerated = ValueGenerated.OnAdd).Message);
        }
    private static void AddSmartEnumAndKeyedValueObjects(
        bool validateOnWrite,
        Dictionary <Type, ValueConverter> converterLookup,
        Action <IMutableProperty> configure,
        IMutableEntityType entity)
    {
        foreach (var propertyInfo in entity.ClrType.GetRuntimeProperties())
        {
            // will be handled by "AddConvertersForNavigations"
            if (entity.FindNavigation(propertyInfo) is not null)
            {
                continue;
            }

            // wil be handled by AddConverterForScalarProperties
            if (entity.FindProperty(propertyInfo) is not null)
            {
                continue;
            }

            if (!propertyInfo.IsCandidateProperty())
            {
                continue;
            }

            var propertyType = propertyInfo.PropertyType;
            var metadata     = ValueObjectMetadataLookup.Find(propertyType);

            if (metadata is null)
            {
                continue;
            }

            var property = entity.AddProperty(propertyInfo);

            SetConverterAndExecuteCallback(validateOnWrite, converterLookup, configure, property);
        }
    }
    private static void AddNonKeyedValueObjectMembers(IMutableEntityType entity)
    {
        if (entity.ClrType.GetCustomAttribute <KeyedValueObjectAttribute>() is not null)
        {
            return;
        }

        var ctorAttr = entity.ClrType.GetCustomAttribute <ValueObjectConstructorAttribute>();

        if (ctorAttr is null || ctorAttr.Members.Length == 0)
        {
            return;
        }

        foreach (var memberName in ctorAttr.Members)
        {
            var property = entity.FindProperty(memberName);

            if (property is null)
            {
                entity.AddProperty(memberName);
            }
        }
    }
        public void SetPrimaryKey(IMutableEntityType entityType)
        {
            var property = entityType.AddProperty("Id", typeof(int));

            entityType.SetPrimaryKey(property);
        }
Exemplo n.º 15
0
 /// <summary>
 ///     Gets the property with the given name, or creates a new one if one is not already defined.
 /// </summary>
 /// <param name="entityType"> The entity type to get or add the property to. </param>
 /// <param name="propertyInfo"> The corresponding property in the entity class. </param>
 /// <returns> The existing or newly created property. </returns>
 /// <remarks> The returned property might not have the specified type and shadowness. </remarks>
 public static IMutableProperty GetOrAddProperty([NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo)
 => entityType.FindProperty(propertyInfo) ?? entityType.AddProperty(propertyInfo);
Exemplo n.º 16
0
 /// <summary>
 ///     Gets the property with the given name, or creates a new one if one is not already defined.
 /// </summary>
 /// <param name="entityType"> The entity type to get or add the property to. </param>
 /// <param name="name"> The name of the property. </param>
 /// <param name="propertyType"> The type of value the property will hold. </param>
 /// <returns> The existing or newly created property. </returns>
 /// <remarks> The returned property might not have the specified type and shadowness. </remarks>
 public static IMutableProperty GetOrAddProperty(
     [NotNull] this IMutableEntityType entityType, [NotNull] string name, [CanBeNull] Type propertyType)
 => entityType.FindProperty(name) ?? entityType.AddProperty(name, propertyType);
 /// <summary>
 ///     Adds a property to this entity type.
 /// </summary>
 /// <param name="entityType"> The entity type to add the property to. </param>
 /// <param name="name"> The name of the property to add. </param>
 /// <param name="propertyType"> The type of value the property will hold. </param>
 /// <returns> The newly created property. </returns>
 public static IMutableProperty AddProperty(
     [NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType)
 => entityType.AddProperty(name, propertyType, null);
Exemplo n.º 18
0
 public void SetPrimaryKey(IMutableEntityType entityType)
 => entityType.SetPrimaryKey(entityType.AddProperty("Id", typeof(int)));