예제 #1
0
        /// <summary>
        ///     Called after an entity type is added to the model.
        /// </summary>
        /// <param name="entityTypeBuilder"> The builder for the entity type. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public virtual void ProcessEntityTypeAdded(
            IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext <IConventionEntityTypeBuilder> context)
        {
            var entityType = entityTypeBuilder.Metadata;
            var clrType    = entityType.ClrType;

            if (clrType == null ||
                entityType.HasDefiningNavigation() ||
                entityType.FindDeclaredOwnership() != null ||
                entityType.Model.FindIsOwnedConfigurationSource(clrType) != null)
            {
                return;
            }

            var baseEntityType = FindClosestBaseType(entityType);

            if (baseEntityType?.HasDefiningNavigation() == false)
            {
                entityTypeBuilder = entityTypeBuilder.HasBaseType(baseEntityType);
                if (entityTypeBuilder != null)
                {
                    context.StopProcessingIfChanged(entityTypeBuilder);
                }
            }
        }
예제 #2
0
    private static void ProcessEntityType(
        IConventionEntityTypeBuilder entityTypeBuilder,
        IConventionContext context)
    {
        var entityType      = entityTypeBuilder.Metadata;
        var model           = entityType.Model;
        var derivedTypesMap = (Dictionary <Type, List <IConventionEntityType> >?)model[CoreAnnotationNames.DerivedTypes];

        if (derivedTypesMap == null)
        {
            derivedTypesMap = new Dictionary <Type, List <IConventionEntityType> >();
            model.SetAnnotation(CoreAnnotationNames.DerivedTypes, derivedTypesMap);
        }

        var clrType  = entityType.ClrType;
        var baseType = clrType.BaseType !;

        if (derivedTypesMap.TryGetValue(clrType, out var derivedTypes))
        {
            foreach (var derivedType in derivedTypes)
            {
                if (!derivedType.IsOwned())
                {
                    derivedType.Builder.HasBaseType(entityType);
                }

                var otherBaseType = baseType;
                while (otherBaseType != typeof(object))
                {
                    if (derivedTypesMap.TryGetValue(otherBaseType, out var otherDerivedTypes))
                    {
                        otherDerivedTypes.Remove(derivedType);
                    }

                    otherBaseType = otherBaseType.BaseType !;
                }
            }

            derivedTypesMap.Remove(clrType);
        }

        if (baseType == typeof(object))
        {
            return;
        }

        IConventionEntityType?baseEntityType = null;

        while (baseEntityType == null &&
               baseType != typeof(object) &&
               baseType != null)
        {
            baseEntityType = model.FindEntityType(baseType);
            if (baseEntityType == null)
            {
                derivedTypesMap.GetOrAddNew(baseType).Add(entityType);
            }

            baseType = baseType.BaseType;
        }

        if (baseEntityType == null)
        {
            return;
        }

        if (!baseEntityType.HasSharedClrType &&
            !baseEntityType.IsOwned())
        {
            entityTypeBuilder.HasBaseType(baseEntityType);
        }
    }
예제 #3
0
        /// <inheritdoc />
        public virtual void ProcessEntityTypeAdded(
            IConventionEntityTypeBuilder entityTypeBuilder,
            IConventionContext <IConventionEntityTypeBuilder> context)
        {
            var entityType = entityTypeBuilder.Metadata;
            var clrType    = entityType.ClrType;

            if (clrType == null ||
                entityType.HasSharedClrType ||
                entityType.Model.IsOwned(clrType) ||
                entityType.FindDeclaredOwnership() != null)
            {
                return;
            }

            var model           = entityType.Model;
            var derivedTypesMap = (Dictionary <Type, List <IConventionEntityType> >)model[CoreAnnotationNames.DerivedTypes];

            if (derivedTypesMap == null)
            {
                derivedTypesMap = new Dictionary <Type, List <IConventionEntityType> >();
                model.SetAnnotation(CoreAnnotationNames.DerivedTypes, derivedTypesMap);
            }

            var baseType = clrType.BaseType;

            if (derivedTypesMap.TryGetValue(clrType, out var derivedTypes))
            {
                foreach (var derivedType in derivedTypes)
                {
                    derivedType.Builder.HasBaseType(entityType);

                    var otherBaseType = baseType;
                    while (otherBaseType != typeof(object))
                    {
                        if (derivedTypesMap.TryGetValue(otherBaseType, out var otherDerivedTypes))
                        {
                            otherDerivedTypes.Remove(derivedType);
                        }

                        otherBaseType = otherBaseType.BaseType;
                    }
                }

                derivedTypesMap.Remove(clrType);
            }

            if (baseType == typeof(object))
            {
                return;
            }

            IConventionEntityType baseEntityType = null;

            while (baseEntityType == null &&
                   baseType != typeof(object) &&
                   baseType != null)
            {
                baseEntityType = model.FindEntityType(baseType);
                if (baseEntityType == null)
                {
                    derivedTypesMap.GetOrAddNew(baseType).Add(entityType);
                }

                baseType = baseType.BaseType;
            }

            if (baseEntityType == null)
            {
                return;
            }

            if (!baseEntityType.HasSharedClrType &&
                baseEntityType.FindOwnership() == null)
            {
                entityTypeBuilder = entityTypeBuilder.HasBaseType(baseEntityType);
                if (entityTypeBuilder != null)
                {
                    context.StopProcessingIfChanged(entityTypeBuilder);
                }
            }
        }