Пример #1
0
        private InternalEntityTypeBuilder Entity(
            TypeIdentity type, ConfigurationSource configurationSource, bool throwOnQuery)
        {
            if (IsIgnored(type, configurationSource))
            {
                return(null);
            }

            var clrType    = type.Type;
            var entityType = clrType == null
                ? Metadata.FindEntityType(type.Name)
                : Metadata.FindEntityType(clrType);

            if (entityType == null)
            {
                if (clrType == null)
                {
                    if (Metadata.ShouldBeOwnedType(type.Name) &&
                        Metadata.HasEntityTypeWithDefiningNavigation(type.Name))
                    {
                        Debug.Assert(configurationSource == ConfigurationSource.Explicit,
                                     "If a type is marked as an owned entity it can only be configured as a non-owned entity type explicitly");

                        Metadata.UnmarkAsOwnedType(type.Name);

                        using (Metadata.ConventionDispatcher.StartBatch())
                        {
                            foreach (var entityTypeWithDefiningNavigation in Metadata.GetEntityTypes(type.Name).ToList())
                            {
                                if (entityTypeWithDefiningNavigation.GetConfigurationSource() != ConfigurationSource.Explicit)
                                {
                                    RemoveEntityType(entityTypeWithDefiningNavigation, configurationSource);
                                }
                            }

                            return(Entity(type, configurationSource, throwOnQuery));
                        }
                    }

                    Metadata.Unignore(type.Name);

                    entityType = Metadata.AddEntityType(type.Name, configurationSource);
                }
                else
                {
                    if (Metadata.ShouldBeOwnedType(clrType) &&
                        Metadata.HasEntityTypeWithDefiningNavigation(clrType))
                    {
                        Debug.Assert(configurationSource == ConfigurationSource.Explicit,
                                     "If a type is marked as an owned entity it can only be configured as a non-owned entity type explicitly");

                        Metadata.UnmarkAsOwnedType(clrType);

                        using (Metadata.ConventionDispatcher.StartBatch())
                        {
                            foreach (var entityTypeWithDefiningNavigation in Metadata.GetEntityTypes(clrType).ToList())
                            {
                                if (entityTypeWithDefiningNavigation.GetConfigurationSource() != ConfigurationSource.Explicit)
                                {
                                    RemoveEntityType(entityTypeWithDefiningNavigation, configurationSource);
                                }
                            }

                            return(Entity(type, configurationSource, throwOnQuery));
                        }
                    }

                    Metadata.Unignore(clrType);

                    entityType = Metadata.AddEntityType(clrType, configurationSource);
                }
            }
            else
            {
                if (throwOnQuery && entityType.IsQueryType)
                {
                    throw new InvalidOperationException(
                              CoreStrings.CannotAccessQueryAsEntity(entityType.DisplayName()));
                }

                entityType.UpdateConfigurationSource(configurationSource);
            }

            return(entityType?.Builder);
        }
Пример #2
0
        private InternalEntityTypeBuilder Entity(
            TypeIdentity type,
            string definingNavigationName,
            EntityType definingEntityType,
            ConfigurationSource configurationSource)
        {
            if (IsIgnored(type, configurationSource))
            {
                return(null);
            }

            var clrType        = type.Type;
            var weakEntityType = clrType == null
                ? Metadata.FindEntityType(type.Name, definingNavigationName, definingEntityType)
                : Metadata.FindEntityType(clrType, definingNavigationName, definingEntityType);

            if (weakEntityType == null)
            {
                var entityType = clrType == null
                    ? Metadata.FindEntityType(type.Name)
                    : Metadata.FindEntityType(clrType);

                IConventionBatch   batch = null;
                EntityTypeSnapshot entityTypeSnapshot = null;
                if (entityType != null)
                {
                    if (!configurationSource.Overrides(entityType.GetConfigurationSource()))
                    {
                        return(null);
                    }

                    batch = ModelBuilder.Metadata.ConventionDispatcher.StartBatch();
                    entityTypeSnapshot = InternalEntityTypeBuilder.DetachAllMembers(entityType);

                    Ignore(entityType, configurationSource);
                }

                if (clrType == null)
                {
                    Metadata.Unignore(type.Name);

                    weakEntityType = Metadata.AddEntityType(type.Name, definingNavigationName, definingEntityType, configurationSource);
                }
                else
                {
                    Metadata.Unignore(clrType);

                    weakEntityType = Metadata.AddEntityType(clrType, definingNavigationName, definingEntityType, configurationSource);
                }

                if (batch != null)
                {
                    entityTypeSnapshot.Attach(weakEntityType.Builder);
                    batch.Dispose();
                }
            }
            else
            {
                weakEntityType.UpdateConfigurationSource(configurationSource);
            }

            return(weakEntityType?.Builder);
        }